Leecode 1048. Longest String Chain
1048. Longest String Chain
You are given an array of words where each word consists of lowercase English letters.
wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.
For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".
A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.
Return the lenth of the longest possible word chain with words chosen from the given list of words.
题目大意
给定一个由小写英文字母组成的单词数组 words
,定义 “前驱” 关系:若能在 wordA
中恰好插入一个字符(不改变原有字符顺序)得到 wordB
,则 wordA
是 wordB
的前驱。
“单词链” 是由单词组成的序列 [word1, word2, ..., wordk]
(k≥1),满足 word1
是 word2
的前驱、word2
是 word3
的前驱,以此类推。单个单词默认是长度为 1 的单词链。
要求返回从给定单词中选出的最长单词链的长度。
示例
- 示例 1:
输入:words = ["a","b","ba","bca","bda","bdca"]
输出:4
解释:最长单词链之一是["a","ba","bda","bdca"]
- 示例 2:
输入:words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
输出:5
解释:所有单词可组成链["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"]
- 示例 3:
输入:words = ["abcd","dbqca"]
输出:1
解释:最长链是单个单词(如["abcd"]
),两单词无法形成链(字符顺序改变)
解题思路
采用了递归 + 记忆化的方法来解决最长字符串链问题
- 先将所有单词存入哈希表,便于快速判断某个字符串是否存在
- 对每个单词,递归计算以它为起点的最长字符串链长度
- 使用记忆化技术存储已计算的结果,避免重复计算
1 | class Solution { |