原题链接在这里: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. .net core获取数据库连接 抛出The type initializer to throw an exception

    原文:https://www.cnblogs.com/pudefu/p/7580722.html 在.NET Framework框架时代我们的应用配置内容一般都是写在Web.config或者App.c ...

  2. Sqlserver 总结(2) 存储过程

    目录 写在前面 什么是存储过程 存储过程的优点 存储过程的分类 1.只返回单一记录集的存储过程 2.没有输入输出的存储过程 3.有返回值的存储过程 4.有输入参数和输出参数的存储过程 5.同时具有返回 ...

  3. igel udc2 config

    igel udc2 config 系统安装盘下载地址 http://www.myigel.biz/?forcedownload /config/bin/igelone_config #!/bin/sh ...

  4. Java之路---Day06

    2019-10-20-23:13:52 目录内容: 1.Scanner类 2.匿名对象 3.Random类 4.对象数组 Scanner类 功能:实现键盘输入数据到程序中 使用步骤: 1.导包: im ...

  5. 易百教程人工智能python修正-人工智能无监督学习(聚类)

    无监督机器学习算法没有任何监督者提供任何指导. 这就是为什么它们与真正的人工智能紧密结合的原因. 在无人监督的学习中,没有正确的答案,也没有监督者指导. 算法需要发现用于学习的有趣数据模式. 什么是聚 ...

  6. 手写MQ框架(一)-准备启程

    一.背景 很久以前写了DAO框架和MVC框架,前段时间又重写了DAO框架-GDAO(手写DAO框架(一)-从“1”开始,源码:https://github.com/shuimutong/gdao.gi ...

  7. vuecli3的项目搭建

    1.卸载旧版本 npm uninstall vue-cli -g 或者 yarn global remove vue-cli 2.安装cli3脚手架 npm install -g @vue/cli 或 ...

  8. nodeJS从入门到进阶三(MongoDB数据库)

    一.MongoDB数据库 1.概念 数据库(DataBase)是一个按照数据结构进行数据的组织,管理,存放数据的仓库. 2.关系型数据库 按照关系模型存储的数据库,数据与数据之间的关系非常密切,可以实 ...

  9. Android Scroller详解

    在学习使用Scroller之前,需要明白scrollTo().scrollBy()方法. 一.View的scrollTo().scrollBy() scrollTo.scrollBy方法是View中的 ...

  10. 图说jdk1.8新特性(1)--- 函数式接口

    函数式接口 总结起来就以下几点: 如果一个接口要想成为函数接口(函数接口可以直接用lambda方式简化),则必须有且仅有一个抽象的方法(非default和static) 可以通过注解@Function ...