[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 ...
随机推荐
- C++中的string详解
标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string> 声明一个字符串 声明一个字符串有很多种方式,具体如 ...
- BZOJ 2285 [Sdoi2011]保密
题解: 求比值用分数规划,单个求太慢了套整体二分 然后求二分图最小割 // luogu-judger-enable-o2 #include<iostream> #include<cs ...
- lvs和keepalived
LVS调度算法参考 RR:轮询 WRR :加权轮询 DH :目标地址哈希 SH:源地址hash LC:最少连接 WLC:加权最少连接,默认 SED:最少期望延迟 NQ:从不排队调度方法 LBLC:基于 ...
- Codeforces Round #594 (Div. 1) Ivan the Fool and the Probability Theory
题意:给你一个NxM的图,让你求有多少符合 “一个格子最多只有一个同颜色邻居”的图? 题解:首先我们可以分析一维,很容易就可以知道这是一个斐波那契计数 因为dp[1][m]可以是dp[1][m-1]添 ...
- Mysql 3306 被 linux 防火墙拦截
项目测试时需要本地连接linux服务器的mysql, 发现navicat无法连接 原因一:mysql没有添加外部ip的访问权限. 原因二:mysql 的 3306 端口 处于被防火墙的拦截状态. 解决 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. MySQL临时表只在当前连接可见,如果使用PHP脚本来创建My ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL PHP 语法
MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,Mysql在PHP的web开发中是应用最广泛. PHP提供了多种方式来访问和操作Mysql数据库记 ...
- 用tkinter写一个记事本程序(未完成)
之前在看tkinter与python编程 ,后面学opengl就把那本书搁置了.几天没用tkinter,怕是基本的创建组件那些都忘记了,所以想着用tkinter试着写一下记事本程序.一开始的时候以为很 ...
- Transmission添加SSL访问
0.准备工作 0.1.在App Center中安装Entware-ng 0.2.以admin用户登录SSH到NAS 0.3.申请SSL证书,可以找免费的申请一个 0.4.公网IP和域名,这个要和SSL ...
- 正确返回Unicode码点大于0xFFFF的字符串长度
如下: function codePointLength(text){ var result = text.match(/[\s\S]/gu); return result? result.lengt ...