【leetcode】1048. Longest String Chain
题目如下:
Given a list of words, each word consists of English lowercase letters.
Let's say
word1is a predecessor ofword2if and only if we can add exactly one letter anywhere inword1to 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_1is a predecessor ofword_2,word_2is 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 <= 10001 <= words[i].length <= 16words[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 方法三 ...
随机推荐
- Glide 图片框架
学习参考:https://blog.csdn.net/guolin_blog/article/details/53759439 一 基础使用 Picasso比Glide更加简洁和轻量,Glide比Pi ...
- SQL数据库字段添加说明文字
1.查看指定表中的所有带说明文字的字段内容 SELECT *,OBJECT_NAME(major_id) AS obj_name FROM sys.extended_properties WHERE ...
- leetcode 46 全排列 (python)
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] ...
- Spring Cloud Stream 使用延迟消息实现定时任务(RabbitMQ)
应用场景 通常在应用开发中我们会碰到定时任务的需求,比如未付款订单,超过一定时间后,系统自动取消订单并释放占有物品. 许多同学的第一反应就是通过spring的schedule定时任务轮询数据库来实现, ...
- 002-es5.4.3结合spring-data-elasticsearch3.0.0.0使用
一.使用前设置 1.elasticsearch开启 cmd下,进入安装目录 cd D:\developToool\elasticsearch-5.4.3 elasticsearch # 或者后台运行 ...
- 网页禁用表单的自动完成功能禁用密码自动填充autocomplete
网页中表单的自动完成功能,有时候很方便,但是有时候并不想让浏览器记忆表单,比如禁用密码域自动填充功能, 网页禁用表单的自动完成功能是由input元素的autocomplete属性控制,关闭表单的自动完 ...
- python修改文件
文档username.txt 将文件中密码123456改成67890: 方法一:(简单粗暴) 1.打开文件 2.读出数据 3.修改数据 4.清空原来文件,将新的内容写进去 f = open('user ...
- Mac入门--通过homebrew下载过慢问题
使用国内的镜像替换homebrew镜像,对镜像进行加速源 原先我们执行brew命令安装的时候,跟3个仓库地址有关 1 brew.git 2 homebrew-core.git 3 homebrew-b ...
- python基础循环语句练习
1.使用while循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n < 11: if n == 7: pass else: print(n) n = n + 1 ...
- LLVM思想与功能综述
llvm似乎还有一个奇怪的优化方法:llvm(low level virtual machine)本身就是一种抽象的.虚拟的计算机架构,其特性介于RISC和CISC之间,llvm会先将代码编译为llv ...