1143. Longest Common Subsequence
Description:
Given two strings text1
and text2
, 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.
Solution:
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int: n1 = len(text1)
n2 = len(text2) dp = [[0 for i in range(n2+1)] for j in range(n1+1)]
for i in range(n1):
for j in range(n2):
if text1[i]==text2[j]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[n1-1][n2-1]
Notes:
2-d Dynamic Programming
O(mn)
1143. Longest Common Subsequence的更多相关文章
- LeetCode 1143. Longest Common Subsequence
原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...
- 【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
随机推荐
- Mysql主键外键操作
外键: ·一对多 ·多对多 ·一对一 ·修改表 ·复制表 主键: rimary key auto_increment(primary key是主键通常和auto_increment自动增加混合 ...
- js替换所有匹配字符
js没有replaceAll,所以用正则表达式查找所有匹配项("g"), .replace(new RegExp("\\.", "g"), ...
- centos useradd 命令详解
useradd 命令 Usage: useradd [options] LOGIN useradd -D useradd -D [options] Options: -b, --base-dir BA ...
- Django的安装、使用详解、自动化测试应用以及程序打包
1.Django的安装 pip install Django 验证 Django 是否能被 Python 识别 >>> import django >>> prin ...
- linux服务器上部署springboot项目,并让他持续运行到后台
我们知道在运行springboot 项目只需要java -jar + 项目的war包(jar包)名. 但是只要终端已停止那么服务就会被关闭,也就无法访问到我们的项目了.所以我们可以使用守护进程的方式来 ...
- Windows server 2012文件服务器配置
文件服务器的管理 Windows server 2012提供了易于使用的管理工具,让系统管理员更有效的管理服务器的资源. 安装文件服务器管理工具 添加角色-安装管理器 安装完成后直接可以在工具中打 ...
- 关于DLL搜索路径的顺序问题
DLL的动态链接有两种方法.一种是加载时动态链接(Load_time dynamic linking).Windows搜索要装入的DLL时,按以下顺序:应用程序所在目录→当前目录→Windows SY ...
- Python(一):一行解法参考
#一行快速排序quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in arra ...
- Codeforces Round #622 (Div. 2)
A: 题意: 有ABC的三种菜,现在有a个A,b个B,c个C,问能组成多少种不同菜单 思路: abc都大于等于4,肯定是7种,给abc排个序,从大到小举例删减 #include<bits/std ...
- Angular 调用百度地图API接口
Angular 调用百度地图API接口 参考原文:https://blog.csdn.net/yuyinghua0302/article/details/80624274 下面简单介绍一下如何在Ang ...