【leetcode】1048. Longest String Chain
题目如下:
Given a list of words, each word consists of English lowercase letters.
Let's say
word1
is a predecessor ofword2
if and only if we can add exactly one letter anywhere inword1
to make it equal toword2
. For example,"abc"
is a predecessor of"abac"
.A word chain is a sequence of words
[word_1, word_2, ..., word_k]
withk >= 1
, whereword_1
is a predecessor ofword_2
,word_2
is a predecessor ofword_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 <= 1000
1 <= words[i].length <= 16
words[i]
only consists of English lowercase letters.
解题思路:典型的动态规划的场景。设dp[i]为word[i]的单词链最长距离,如果word[i]是word[j]的predecessor,那么有dp[i] = max(dp[i], dp[j] + 1)。至于如何判断word[i]是否是word[j]的predecessor?对两个单词进行逐位比较,只允许有某一个的字符不一样。
代码如下:
class Solution(object):
def longestStrChain(self, words):
"""
:type words: List[str]
:rtype: int
"""
def isPredecessor(s1,s2):
add = False
s1_inx = 0
s2_inx = 0
while s1_inx < len(s1) and s2_inx < len(s2):
if s1[s1_inx] == s2[s2_inx]:
s1_inx += 1
s2_inx += 1
elif add == False:
s2_inx += 1
add = True
else:
return False
return True
words.sort(cmp=lambda x,y:len(x) - len(y))
dp = [1] * len(words)
res = 1
for i in range(len(words)):
for j in range(0,i):
if len(words[i]) == len(words[j]):
break
elif len(words[i]) - 1 > len(words[j]):
continue
else:
if isPredecessor(words[j],words[i]):
dp[i] = max(dp[i],dp[j]+1)
res = max(res,dp[i])
#print words
#print dp
return res
【leetcode】1048. Longest String Chain的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode 1048. Longest String Chain
原题链接在这里:https://leetcode.com/problems/longest-string-chain/ 题目: Given a list of words, each word con ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- [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】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
随机推荐
- Linux动态库和静态库
Linux下动态库查看办法:nm -D libavformat.so Linux下静态库查看办法:ar -t libavformat.a ------------------------------- ...
- 职位-CHO:CHO
ylbtech-职位-CHO:CHO 首席人力资源官(Chief Human Resource Officer),简称CHO.是具有制定集团化经营的公司人力资源的战略规划,并监督执行,负责建立畅通的沟 ...
- java poi 导入导出Excel xsl xslx
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- spring的基础配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【Unity Shader】---入门知识点
着色器声明(“名字”)Shader "ShaderDiffuseExample" { 一.属性定义(作用:外部传入参数) 属性定义语法:PropName("Display ...
- DP---DAG、背包、LIS、LCS
DP是真的难啊,感觉始终不入门路,还是太弱了┭┮﹏┭┮ DAG上的DP 一般而言,题目中如果存在明显的严格偏序关系,并且求依靠此关系的最大/最小值,那么考虑是求DAG上的最短路或者是最长路.(据说 ...
- Reinforcement Learning Index Page
Reinforcement Learning Posts Step-by-step from Markov Property to Markov Decision Process Markov Dec ...
- Java web 加载过程
1.Web容器初始化过程 2.SpringMVC中web.xml配置 3.认识ServletContextListener 4.认识ContextLoaderListener 5.Dispatcher ...
- nested exception is java.lang.NoClassDefFoundError: org/hibernate/validator/resourceloading/ResourceBundleLocator
原来的hibernate-validator-5.3.0.Final.jar里没有这个接口,换成 hibernate-validator-4.1.0.Final.jar 就好了
- ubuntu终端代理之proxychains
命令行代理 安装proxychains sudo apt install proxychains 配置proxychains sudo vim /etc/proxychains.conf 在proxy ...