原题链接在这里:https://leetcode.com/problems/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".

word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2word_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".

Note:

  1. 1 <= words.length <= 1000
  2. 1 <= words[i].length <= 16
  3. words[i] only consists of English lowercase letters.

题解:

Sort the words based on word length.

For each word, find all its possible predecessors by deleting one char from the word, if it exist, update longest chain length up to this word.

And put it in the mapping.

At the same time, update the global longest res.

Time Complexity: O(nlogn + n*len). n is the words count. len is the average length of word.

Space: O(n).

AC Java:

 class Solution {
public int longestStrChain(String[] words) {
HashMap<String, Integer> hm = new HashMap<>();
Arrays.sort(words, (a, b) -> a.length() - b.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, hm.getOrDefault(prev, 0)+1);
} hm.put(word, best);
res = Math.max(res, best);
} return res;
}
}

LeetCode 1048. Longest String Chain的更多相关文章

  1. 【leetcode】1048. Longest String Chain

    题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...

  2. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

  3. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...

  4. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

  5. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  6. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  7. #Leetcode# 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...

  8. [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  9. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

随机推荐

  1. MVC路由规则

    1 可以创建多条路由规则,每条路由规则的那么属性不同 2路由规则是有顺序的.如果被前面的规则匹配了,那么后面的规则就没机会了 3 constraints 约束: 4namespaces 命名空间 5r ...

  2. 7. Spark SQL的运行原理

    7.1 Spark SQL运行架构 Spark SQL对SQL语句的处理和关系型数据库类似,即词法/语法解析.绑定.优化.执行.Spark SQL会先将SQL语句解析成一棵树,然后使用规则(Rule) ...

  3. 【leetcode-97 动态规划】 交错字符串

    (1过,调试很久) 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...

  4. C#字符串连接问题(包含破折号和引号)

    1.需求场景: 生成字符串如下:jsonStr.Append("\"SensorTypes\":"); 解决方法: string code = @"j ...

  5. 关于使用jquery form submit出现多次提交的问题

    错误的写法: $(this).submit(function () { $(this).ajaxSubmit({ url: opts.url, type: 'post', dataType: 'jso ...

  6. 非洲affrike单词

    affrike 英文单词,含义是非洲,非洲大陆. 中文名:非洲 外文名:affrike 目录 释义 affrike noun名词 非洲,也用做africa 1.Word Origin and Hist ...

  7. MySQL Replication--复制基本工作原理

    复制工作原理(1) master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events):(2) slave将master的binary lo ...

  8. docker 安装prometheus

    使用到的命令: [root@lgswork ~]# docker search prometheus NAME DESCRIPTION STARS OFFICIAL AUTOMATED prom/pr ...

  9. PHP控制结构if else,while,do while,for,foreach,break ,continue

    php中控制结构有 控制结构 判断分支 if else, 三元运算符,  switch case, 循环 while do while for foreach 跳出 break continue // ...

  10. springdata jpa 关于分页@Query问题

    关于springdata jpa 分页问题相信很多小伙伴都遇到过,只要表中数量到达分页条件就会报错 废话少说直接上代码: @Query(nativeQuery = true, value = &quo ...