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".
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的更多相关文章

  1. LeetCode 1048. Longest String Chain

    原题链接在这里:https://leetcode.com/problems/longest-string-chain/ 题目: Given a list of words, each word con ...

  2. 【leetcode】1048. Longest String Chain

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

  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. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. LC 3. Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  6. [LC] 5. Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  7. [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 ...

  8. [LC] 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LC] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

随机推荐

  1. 一天一个设计模式——工厂方法(FactoryMethod)模式

    一.模式说明 在前一个模板方法(Template Method)模式中,父类定义了处理流程,而流程中用到的方法交给子类去实现.类似的,在工厂方法模式中,父类决定如何生成实例,但并不决定所要生成的具体类 ...

  2. torch.Tensor文档学习笔记

    A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. 张量(torch.Ten ...

  3. 为什么ApplePay在中国一直火不起来?

    今年7月,易观发布<中国第三方移动支付市场2018年第1季度监测报告>.报告显示,2018年第一季度支付宝以53.76%的市场份额占据移动支付头把交椅,腾讯金融(微信支付)则以38.95% ...

  4. GitHub练习——如何将本地已有项目添加到github

    刚开始开始接触,搞点简单的,看看是怎么把项目传上去,总结一下,大概是这些步骤: 创建本地仓库 将本地仓库变成git可管理的仓库:git init 把项目文件添加到缓存区:项目文件添加到已有的仓库,然后 ...

  5. Java编程知识点梳理

    1. elementAt()   temp.elementAt(0) 返回temp这个vector里面存放的第一个元素--->也是一个vector类型. 2. 字符串空格分割 String [] ...

  6. day68-CSS-float浮动,clear清除浮动,overflow溢出

    1. float 浮动 1.1 在 CSS 中,任何元素都可以浮动. 1.2 浮动元素会生成一个块级框,而不论它本身是何种元素.内联标签设置浮动,就变成了块级标签. 1.3 关于浮动的两个特点: 浮动 ...

  7. 04-for循环的各个语句及list列表学习

    目录 04-for循环的各个语句及list列表学习 1. for循环 2. range()函数 3. 循环语句中的break.continue.pass 4. list列表 5. 列表生成式 6. 实 ...

  8. PAT B1038 统计同成绩学生超时问题

    输入格式: 输入在第 1 行给出不超过 10​5​​ 的正整数 N,即学生总人数.随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔.最后一行给出要查询的分数个数 K(不超过 N 的正整数),随 ...

  9. linux.linuxidc.com - /2011年资料/Android入门教程/

    本文转自 http://itindex.net/detail/15843-linux.linuxidc.com-%E8%B5%84%E6%96%99-android Shared by Yuan 用户 ...

  10. 微信小程序使用第三方FontIcon库的部分字体图标

    一.提取部分图标重新制作TTF字库 我没有使用网上大多数文章写的淘宝提供的fonticon,而只使用了Ionicons的几个图标,所以打开Ionicons的官网点击右上角的Designer pack下 ...