[LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
class Solution {
    public int longestStrChain(String[] words) {
        Arrays.sort(words, (a, b) -> a.length() - b.length());
        // map is used as space for dp best[i] = 1 + best[i- 1]
        Map<String, Integer> map = new HashMap<>();
        int res = 0;
        for (String word: words) {
            map.put(word, 1);
            for (int i = 0; i < word.length(); i++) {
                String prev = word.substring(0, i) + word.substring(i + 1);
                int curLen = map.get(word);
                if (map.containsKey(prev) && map.get(prev) + 1 > curLen) {
                    map.put(word, map.get(prev) + 1);
                }
            }
            res = Math.max(res, map.get(word));
        }
        return res;
    }
}
[LC] 1048. Longest String Chain的更多相关文章
- LeetCode 1048. Longest String Chain
		
原题链接在这里:https://leetcode.com/problems/longest-string-chain/ 题目: Given a list of words, each word con ...
 - 【leetcode】1048. Longest String Chain
		
题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...
 - leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]
		
1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...
 - LC 516. Longest Palindromic Subsequence
		
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
 - LC 3. Longest Substring Without Repeating Characters
		
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
 - [LC] 5. Longest Palindromic Substring
		
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
 - [LC] 159. Longest Substring with At Most Two Distinct Characters
		
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
 - [LC] 32. Longest Valid Parentheses
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - [LC] 14. Longest Common Prefix
		
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
 
随机推荐
- python函数-函数进阶
			
python函数-函数进阶 一.命名空间和作用域 1.命名空间 内置命名空间 —— python解释器 就是python解释器一启动就可以使用的名字存储在内置命名空间中 内置的名字在启动解释器的时候被 ...
 - python刷LeetCode:14. 最长公共前缀
			
难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
 - POJ 1129:Channel Allocation 四色定理+暴力搜索
			
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
 - html—表单控件
			
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
 - 18 12 30  服务器 Django 的初步使用  环境变量的调整
			
1.安装django 1.1.下载Django包 https://www.djangoproject.com/download/https://www.djangoproject.com/m/rele ...
 - codeigniter框架开发技巧
			
codeigniter框架开发技巧 来源:未知 时间:2014-10-20 11:36 阅读数:171 作者:xbdadmin [导读] (1)自动加载modelsphp5风格 使用这个 ...
 - CocoaPods为多个target添加依赖库/Podfile的配置
			
Podfile的相关配置,请看官方文档http://guides.cocoapods.org/syntax/podfile.html 1)多个target公用相同库,还可以添加额外的不同第三方库 编辑 ...
 - xdc如何设置输入延时
			
常用命令: Set_input_delay,create_clock,set_output_delay以及用于组合逻辑的set_max_delay. Input delay: 什么是输入延时? Tra ...
 - UML-UML工具与UML蓝图
			
1.UML应用场景 1).UML作为草图 2).UML作为蓝图. UML生成java代码(前向工程) java代码生成UML(逆向工程) 2.如果绘制了UML草图,如何在编码后更新该图形? 逆向工程, ...
 - sersync配置
			
只记录了自己的操作步骤,详细信息也可以参考https://www.cnblogs.com/sellsa/p/5345092.html 安装sersync 将sersync安装包解压后会有两个配置文件 ...