link to problem

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的更多相关文章

  1. LeetCode 1143. Longest Common Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...

  2. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  5. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

随机推荐

  1. Ninject 2.x细说---1.基本使用

    Ninject 2.x细说---1.基本使用 https://blog.csdn.net/weixin_33809981/article/details/86091159   本来想使用一下Ninje ...

  2. PPT 素材大全

    1.模板大全 2.三方辅助软件APP 3.PPT学习软件 www.presentationload.com dribble.com www.zcool.com.cn 4.其他功能

  3. koa2第一天 安装koa2

    安装全局koa2:npm install -g koa2 -generator 创建一个koa2文件夹:koa2 -e koa2 进入koa2文件夹:cd koa2 安装npm模块:npm insta ...

  4. 常见运算符_python

    一.算数运算符a=2b=5c=8#加减乘除print(a+b,a-b,a*b,a/b)#取模,也就是余数print(b%a)#取整除print(b//a)#幂(次方)print(b**a) 二.比较运 ...

  5. Iris_xorm

    xorm表基本操作及高级操作 表结构基本操作 对表结构的操作最常见的操作是查询和统计相关的方法,我们首先来看相关实现: 条件查询 Id值查询:参数接收主键字段的值.例如: var user User ...

  6. MySQL移动数据库位置

    http://zlyang.blog.51cto.com/1196234/1726029 需求:MySQL数据库文件原位置:/var/lib/mysql 要移动至:/data/mysql 1.首先在/ ...

  7. i5+GT730+B85安装OSX10.10.5 (Yosemite Install(14F27).cdr)

    1.用windows磁盘管理工具分出10G分区,指定盘符,但不格式化 2.管理员身份打开“硬盘安装助手” 3.选择cdr文件,取消3个选择框,然后开始写入 4.有可能需要重置安装分区的磁盘标识为AF ...

  8. SARS病毒

    每一道题目皆是一处美丽的风景: 何为科技的力量和程序的思维哦,在这暑假的编程之路上,我要好好地体验一番来嘞! 数学规律是:f(n)=2^(n-1)+4^(n-1).     //递推分析可得!具体过程 ...

  9. php对字符串的操作

    php最文字的处理很是强大,之前一直云里雾里,这次学习一下. 1,' 与 ”的区别 <?php //双引号中的特殊字符会被解析 echo "你好\t我好";echo &quo ...

  10. 【SIKI学院】愤怒的小鸟创建过程-1

    第一讲:资源导入,场景的简单搭建 1.创建一个2D工程,用到了3个场景,因此ctrl +S创建场景00-level,ctrl+N保存在你创建的文件夹中,这样一个文件就创建好了,之后继续重复此步骤创建另 ...