题目如下:

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.

解题思路:典型的动态规划的场景。设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的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. LeetCode 1048. Longest String Chain

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

  5. 【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 ...

  6. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  7. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

  8. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

随机推荐

  1. codeforces 668C - Little Artem and Random Variable

    题目链接:http://codeforces.com/contest/668/problem/C --------------------------------------------------- ...

  2. 【洛谷P1036 选数】

    这个题显然用到了深搜的内容 让我们跟着代码找思路 #include<bits/stdc++.h>//万能头 ],ans; inline bool prime(int n)//最简单的判定素 ...

  3. 【转】一个 Vim 重度用户总结的 vim 超全指南

    [转]一个 Vim 重度用户总结的 vim 超全指南 我本人是 Vim 的重度使用者,就因为喜欢上这种双手不离键盘就可以操控一切的feel,Vim 可以让我对文本的操作更加精准.高效. 对于未使用过 ...

  4. == 和 equals的区别

    == 和 equals的区别   基本类型:== 比较的是两个变量的面值大小 对象对象: 比较的是内存地址  特例: String a = "abc" String b = &qu ...

  5. CEPH安装(CentOS 7)

    以包含四个节点的集群为例,其中包括一个 ceph-deploy 管理节点和一个三节点的Ceph存储集群. 下图中每个节点代表一台机器. 安装 CEPH 部署工具 执行如下命令: sudo yum in ...

  6. Java开发第一次面试经验(视频面试)

    坐标:山东潍坊公共实训基地 面试岗位:java开发实习生 我们班级一共6个人一起面试,1对1,其他人坐在旁边倾听,两个大牛,四个酱油,我应该是最黑的酱油啦. 面试问题: 1.请简短的做一下自我介绍: ...

  7. HDU 1174 题解(计算几何)

    题面: 爆头 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. JUC并发包基本使用

    一.简介 传统的Java多线程开发中,wait.notify.synchronized等如果不注意使用的话,很容易引起死锁.脏读问题.Java1.5 版本开始增加 java.util.concurre ...

  9. vscode加MinGw三步搭建c/c++调试环境

    vscode加MinGw三步搭建c/c++调试环境 step1:安装vscode.MinGw 1.1 vscod常规安装:https://code.visualstudio.com/ 1.2 MinG ...

  10. 使用hash表进行数组去重

    哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列 ...