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. (简单模拟)P1540 机器翻译

    题解: #include<iostream>#include<cmath>using namespace std; int main(){ int m,n; cin>&g ...

  2. Linux 目录变化监听 - python代码实现

    在python中 文件监控主要有两个库, 一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),pyinotify依赖于Linux平台的in ...

  3. XML--XSL

    参考 http://blog.51cto.com/cnn237111/1345998 https://www.w3.org/TR/2017/REC-xslt-30-20170608/ 声明 把文档声明 ...

  4. Ubuntu无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?

    转自:http://hi.baidu.com/xiaobaishu 鸣谢: xuleibjtu2010的原创空间 1.终端输入 ps  -aux ,列出进程.找到含有apt‘-get的进程,直接sud ...

  5. 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学

    编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...

  6. dp--分组背包 P1757 通天之分组背包

    题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品相互冲突,现在,他想知道最大 ...

  7. vue表单选项框

    选项框选的内容在下面显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  8. python语法基础-并发编程-进程-进程理论和进程的开启

    ############################################## """ 并发编程的相关概念: 进程 1,运行中的程序,就是进程,程序是没有生 ...

  9. 2.react 基础 - create-react-app 目录结构 及 组件应用

    1. react-app 脚手架的 目录结构 node_modules -d 存放 第三方下载的 依赖的包 public -d    资源目录 favicon.ico - 左上角的图标 index.h ...

  10. 基于SSM开发在线考试系统 Java源码

    实现的关于在线考试的功能有:用户前台:用户注册登录.查看考试信息.进行考试.查看考试成绩.查看历史考试记录.回顾已考试卷.修改密码.修改个人信息等,后台管理功能(脚手架功能不在这里列出),科目专业管理 ...