【leetcode】1143. Longest Common Subsequence
题目如下:
Given two strings
text1
andtext2
, return the length of their longest common subsequence.A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.Constraints:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
- The input strings consist of lowercase English characters only.
解题思路:典型的动态规划场景。记dp[i][j]为text1的[0~i]区间,text2[0~j]区间内最长的公共子序列的长度。那么显然有: 如果 text1[i] == text2[j],dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);又如果 text1[i] != text2[j],有 dp[i][j] = max(dp[i][j],dp[i-1][j-1],dp[i-1][j],dp[i][j-1])。
代码如下:
class Solution(object):
def longestCommonSubsequence(self, text1, text2):
"""
:type text1: str
:type text2: str
:rtype: int
"""
dp = [[0]* len(text2) for _ in text1]
for i in range(len(text1)):
for j in range(len(text2)):
if text1[i] == text2[j]:
dp[i][j] = 1
if i > 0 and j > 0:
dp[i][j] = max(dp[i][j],1+dp[i-1][j-1])
else:
if i > 0 and j > 0:
dp[i][j] = max(dp[i][j],dp[i-1][j-1])
if i > 0:
dp[i][j] = max(dp[i][j],dp[i-1][j])
if j > 0:
dp[i][j] = max(dp[i][j],dp[i][j-1])
#print dp
return dp[-1][-1]
【leetcode】1143. Longest Common Subsequence的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【Lintcode】077.Longest Common Subsequence
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
随机推荐
- SpringCloud:(一)服务注册与发现
最近跟着方志明老师学习SpringCloud,博客地址如下: https://blog.csdn.net/forezp/article/details/81040925 自己也跟着撸了一遍,纸上得来终 ...
- vue路由高级用法
五.路由设置高级用法alias 别名 {path:'/list',component:MyList,alias:'/lists'}redirect 重定向 {path:'/productList',r ...
- 应用安全 - CMS - PHPCMS漏洞汇总
CVE-2011-0644 Date: 2011.1 类型: /flash_upload.php SQL注入 影响版本:phpCMS 2008 V2 PHPCMS PHPCMS通杀XSS 在我要报错功 ...
- HashMap中确定数组位置为什么要用hash进行扰动
HashMap数据存储的过程先根据key获得hash值,通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存 ...
- 剑指Offer编程题(Java实现)——链表中环的入口结点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路一 迭代遍历链表,利用HashSet将每个结点添加到哈希表中,如果添加失败(重复遍历了这个结点即遇到环),输出 ...
- echart 柱状图X轴显示不全
z要设置interval为0就可以了 xAxis: [ { type: "category", boundaryGap: false, data: [], axisLine: { ...
- 使用Object.create()实现继承 用 Object.create实现类式继承
使用Object.create()实现继承:https://www.cnblogs.com/cuew1987/p/4075027.html 用 Object.create实现类式继承:https:// ...
- Linux如何设置在当前目录下打开终端
转:https://blog.csdn.net/iot_flower/article/details/71189816 1. sudo apt-get install nautilus-open-te ...
- thinkphp整合Ueditor编辑器
编辑器下载地址:http://ueditor.baidu.com/website/download.html#ueditor 放在项目Public或者入口同级目录均可. 前台代码 <div cl ...
- idea配置less自动编译
参考: idea配置less自动编译 1. 电脑安装node.js环境: window下直接上官网下载node.msi文件下载安装即可 安装完成后在命令行执行如下命令表明安装成功 npm -v nod ...