LeetCode题解(1048):最长字符串链(Python)
发布日期:2021-06-29 20:09:42 浏览次数:3 分类:技术文章

本文共 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

转载地址:https://dataartist.blog.csdn.net/article/details/109844778 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1072):按列翻转得到最大值等行数(Python)
下一篇:LeetCode题解(1044):最长重复子串(Python)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月15日 03时11分10秒