原题链接在这里: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. Python之logging.basicConfig函数各参数

    filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所 ...

  2. 一个基于tcp的socket简单对话小例子

    首先我们需要写连个py文件,一个server,一个client. import socket sk = socket.socket() # sk.bind(('ip',port)) sk.bind(( ...

  3. C++ Primer中文第四版

    C++ Primer中文第四版 在简书上发现有挂羊头卖狗肉的,发的plus,而且压缩包还得付钱获取密码,我直接去github搜到了第四版,在此分享一下.   格式:pdf 书签目录:有   下载地址: ...

  4. Go基础编程实践(八)—— 系统编程

    捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...

  5. jQuery格式化显示json数据

    一.概述 JSONView 在gitlab上面,有一个jQuery JSONView插件,地址为:https://github.com/yesmeck/jquery-jsonview demo地址:h ...

  6. python_封装redis_list方法

    xshell 进入 虚拟环境 安装 redis workon py3env # 进入虚拟环境 pip install redis # 安装redis deactivate # 退出虚拟环境 简单的封装 ...

  7. TCP,UDP 通讯的helper类

    使用Tcp通讯,首先要启动tcp服务端监听客户端,客户端发送消息,服务端收到消息 1.服务端代码如下 public class TcpServerTest { public async Task Be ...

  8. Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)

    Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...

  9. Unicode 字符和UTF编码的理解

    Unicode 编码的由来 我们都知道,计算机的内部全部是由二进制数字0, 1 组成的, 那么计算机就没有办法保存我们的文字, 这怎么行呢? 于是美国人就想了一个办法(计算机是由美国人发明的),也把文 ...

  10. 基于 Express + MySQL + Redis 搭建多用户博客系统

    1. 项目地址 https://github.com/caochangkui/node-express-koa2-project/tree/master/blog-express 2. 项目实现 Ex ...