0
点赞
收藏
分享

微信扫一扫

LeetCode 1048. 最长字符串链

LeetCode 1048. 最长字符串链

文章目录

题目描述

1048. 最长字符串链
提示:

    1 <= words.length <= 1000
    1 <= words[i].length <= 16
    words[i] 仅由小写英文字母组成。

一、解题关键词

数组 words 、最长可能长度 、words[i] 仅由小写英文字母组成

二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

class Solution {
    public int longestStrChain(String[] words) {
        Map<String,Integer> dp = new HashMap<>();
        Arrays.sort(words,(a,b)-> a.length() - b.length());
        int res = 0;
        for(String word : words){
            int maxLen = 0;
            for(int i= 0; i < word.length();i++){
                String prev = word.substring(0,i) + word.substring(i + 1);
                maxLen = Math.max(maxLen,dp.getOrDefault(prev,0) + 1);
            }
            dp.put(word,maxLen);
            res  = Math.max(res,maxLen);
        }
        return res;

    }
}

2.知识点



总结

相同题目

xxx

举报

相关推荐

0 条评论