英文原题
Given a list of words, each word consists of English lowercase letters.
Let’s say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, “abc” is a predecessor of “abac”.
A word chain is a sequence of words [word_1, word_2, …, word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example
1 | Input: words = ["a","b","ba","bca","bda","bdca"] |
分析
比较容易想到用动态规划来做这个题目,先把单词按照长度排序,从前往后记录每个单词以这个单词结尾时最长的单词串多长,后面再碰到的单词则从前面找它的predecessor。 这题有一个小技巧,我们可以用字典来保存,键是单词,值是这个单词为结尾最长的串的长度,这样比用数组保存可以节约很多时间。
python 代码
这个是用list保存的方法:
1 | class Solution: |
还需要判断是不是predecessor,时间复杂度就上来了。
这个是用字典:
1 | class Solution: |