LeetCode 1048. Longest String Chain
原题链接在这里: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".
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".
Note:
1 <= words.length <= 10001 <= words[i].length <= 16words[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的更多相关文章
- 【leetcode】1048. Longest String Chain
题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
- leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]
1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- [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 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
随机推荐
- linux安装 uwsgi 测试 test.py 不显示hello world 的解决办法
一般部署项目到服务器,会安装uwsgi,但是很多教程在安装它的时候会让你测试一下安装好了没,于是就有很多像我一样懵逼的少年掉进一个坑里出不来,很久.很久... 那就是最后浏览器输入ip:8000端口后 ...
- leetcode动态规划笔记一---一维DP
动态规划 刷题方法 告别动态规划,连刷 40 道题,我总结了这些套路,看不懂你打我 - 知乎 北美算法面试的题目分类,按类型和规律刷题 题目分类 一维dp House Robber : 求最大最小值 ...
- SQL Server 2012启动时提示:无效的许可证数据,需要重新安装
因为手咸,觉得电脑没有VS 2010版本的软件,就把Microsoft Visual C++ 2010某个组件给卸载了. 然后打开Sql Server 2012,就开始报错. 重装之后,也还是报错,将 ...
- python入门基础 02
目录 1.while 2.字符串格式化 3.运算符 4.编码初始 总结 1.while # while -- 关键字 (死循环) # # if 条件: # 结果 # # while 条件: # 循环体 ...
- Mac android studio真机调试步骤
Mac android studio真机调试步骤 这些步骤是在百度中查找的,当时记录下来了,过了个把月了,今天写出来,我也重温步骤. 1.找到android的SDK路径,一般是 /user/你的电脑名 ...
- 图片Image转换为base64编码的方法
1.FileReader 通过XMLHttpRequest请求图片Blob数据格式,然后利用FileReader转换为dataURL function toDataURL(url, callback) ...
- Linux排查PHP-FPM进程过量常用命令
命令如下: 查看每个PHP-FPM进程的内存占用:ps -ylC php-fpm –sort:rss 查看消耗内存最多的前 40 个进程:ps auxw|head -1;ps auxw|sort -r ...
- Java 之 Response 发送验证码案例
定义一个 Servlet 用来在内存中生成 二维码图片,并向浏览器页面输出. import javax.imageio.ImageIO; import javax.servlet.ServletExc ...
- 安装仪表盘控件Iocomp会遇到的几个常见问题
相信从事HMI和自动化开发专业的小伙伴在使用工业仪表盘和图表控件时,都很熟悉怎么安装和操作Iocomp产品(毕竟它功能太强大了,基本人手一份的节奏),但是小编想肯定还是有很多和小编一样的入门的伙伴在安 ...
- Android App 架构演变
文:https://www.jianshu.com/p/ce26e7960926 最近App项目(MVC架构)越做越大,协同开发效率较低,维护困难,所以产生了调整架构的想法,在 简书.csdn.知乎上 ...