本文共 998 字,大约阅读时间需要 3 分钟。
题目:(中等)
标签:哈希表、动态规划
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( W l o g W + W × L 2 l o g L ) O(WlogW+W×L^2logL) O(WlogW+W×L2logL) : 其中L为单词长度 | O ( W ) O(W) O(W) | 308ms (31.70%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一(哈希表):
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda x: len(x)) ans = 1 count = { } for word in words: size = len(word) if size == 1: count[word] = 1 else: min_from = float("inf") for i in range(size): sorted_word = "".join(sorted(word[:i] + word[i + 1:])) if sorted_word in count: min_from = min(min_from, count[sorted_word]) if min_from != float("inf"): ans = max(ans, size - min_from + 1) count["".join(sorted(word))] = min_from else: count["".join(sorted(word))] = size return ans
转载地址:http://bczcf.baihongyu.com/