ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1048. 最长字符串链

2022-01-10 11:34:21  阅读:186  来源: 互联网

标签:word String int 1048 length words 字符串 index1 最长


给出一个单词列表,其中每个单词都由小写英文字母组成。

如果我们可以在 word1 的任何地方添加一个字母使其变成 word2,那么我们认为 word1 是 word2 的前身。例如,"abc" 是 "abac" 的前身。

词链是单词 [word_1, word_2, ..., word_k] 组成的序列,k >= 1,其中 word_1 是 word_2 的前身,word_2 是 word_3 的前身,依此类推。

从给定单词列表 words 中选择单词组成词链,返回词链的最长可能长度。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-string-chain
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

记忆化搜索

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {

    private Map<Integer, List<String>> dictList;

    private Map<String, Integer> dp = new HashMap<>();

    private boolean isNext(String w1, String w2) {
        if (w1.length() + 1 != w2.length()) {
            return false;
        }
        int index1 = 0, index2 = 0;
        while (index1 < w1.length() && index2 < w2.length() && w1.charAt(index1) == w2.charAt(index2)) {
            index1++;
            index2++;
        }
        /**
         * 在末尾添加
         */
        if (index1 == w1.length()) {
            return true;
        }

        index2++;
        while (index1 < w1.length() && index2 < w2.length() && w1.charAt(index1) == w2.charAt(index2)) {
            index1++;
            index2++;
        }
        return index1 == w1.length();
    }

    private int solve(String word) {
        if (!dp.containsKey(word)) {
            int nextLength = word.length() + 1;
            List<String> list = dictList.get(nextLength);
            if (list == null) {
                dp.put(word, 1);
            } else {
                int ans = 0;
                for (String item : list) {
                    if (isNext(word, item)) {
                        ans = Math.max(ans, solve(item));
                    }
                }
                dp.put(word, ans + 1);
            }
        }
        return dp.get(word);
    }

    public int longestStrChain(String[] words) {
        if (words == null || words.length == 0) {
            return 0;
        }

        int minLength = Integer.MAX_VALUE;
        int maxLength = Integer.MIN_VALUE;

        Map<Integer, List<String>> dictList = new HashMap<>();
        for (String word : words) {
            dictList.computeIfAbsent(word.length(), k -> new ArrayList<>()).add(word);
            minLength = Math.min(minLength, word.length());
            maxLength = Math.max(maxLength, word.length());
        }
        this.dictList = dictList;
        int ans = 0;

        for (String word : words) {
            ans = Math.max(ans, solve(word));
        }

        return ans;
    }
}

排序 + 动态规划

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

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

标签:word,String,int,1048,length,words,字符串,index1,最长
来源: https://www.cnblogs.com/tianyiya/p/15783713.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有