Mix and Build
Time limit1sMemory limit128 MB
Find the longest sequence of given distinct words where each word is formed by adding one letter and rearranging.
- Level
Medium6 of 10
- Topics
- Hash map, Dynamic programming, String, Sorting
- Solved
- No attempts yet
Problem
You are given a list of words, where each word is a sequence of lowercase letters. From this list, find the longest chain of words in which every is a mixed extension of .
A word is a mixed extension of a word if can be obtained by adding exactly one letter to and then rearranging all of the letters in any order. Equivalently, is a mixed extension of when the multiset of letters of equals the multiset of letters of together with one extra letter (so the length of is exactly one greater than the length of ).
For example, the words ab, bar, crab, cobra, carbon form a chain of length , because each word is a mixed extension of the one before it.
Input
The input contains at least and at most lines. Each line contains one word. Every word has length at least and at most and consists only of lowercase letters. All words are distinct.
Output
Print a single integer: the length of the longest chain (that is, the number of words in it) that can be built from the given words.