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", ...
随机推荐
- kali2018.4 下载地址
https://www.linuxidc.com/Linux/2018-10/155088.htm
- AcWing 125. 耍杂技的牛
//按照wi+si从小到大的顺序排,结果一定最优,最大的危险系数一定是最小的 //类比于国王游戏 #include <iostream> #include <algorithm> ...
- selenium的显示等待、隐式等待
转载:https://www.cnblogs.com/mabingxue/p/10293296.html Selenium显示等待和隐式等待的区别1.selenium的显示等待原理:显示等待,就是明确 ...
- win10 解决.net framework 3.5 安装报错 0x80240438
打开注册表:cmd+r 输入regedit,确定:找到路径HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU ...
- 题解 P2320 【[HNOI2006]鬼谷子的钱袋】
P2320 [HNOI2006]鬼谷子的钱袋 挺有趣的一道题,之所以发这篇题解是因为感觉思路的更清晰一点qwq 此题主要有两种方法: 一.分治思想 例如要凑出1~20,假如我们已经能凑出1~10了,那 ...
- C语言数据结构——第一章 数据结构的概念
一.数据结构的基本概念 1.1-数据结构是什么? 数据结构是计算机存储和组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合.一般情况下,精心选择的数据结构可以带来更高的运行或者 ...
- maven基础学习篇
一.Maven的两大核心功能:依赖管理(主要是jar包的管理) 和 一键构建 1.依赖管理:maven项目所需要的jar包全部放在仓库中,项目只放置jar包的坐标,所要用到的jar包都从仓库中获 ...
- [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅
有一段时间没有更新技术博文了,因为这段时间埋下头来看Vue源码了.本文我们一起通过学习双向绑定原理来分析Vue源码.预计接下来会围绕Vue源码来整理一些文章,如下. 一起来学Vue双向绑定原理-数据劫 ...
- Bugku-CTF之文件包含2 (150)
Day37 文件包含2
- Visual Studio Code修改全屏背景
打开VSCode安装目录,找到workbench.desktop.main.css文件 在最后一行添加以下代码: body{ pointer-events:auto !important; backg ...