当前位置:网站首页>LeetCode 1143. Longest common subsequence
LeetCode 1143. Longest common subsequence
2022-06-13 09:21:00 【Shao_ sen】
1143. Longest common subsequence
difficulty secondary
Given two strings text1
and text2
, Returns the longest of these two strings Common subsequence The length of . If it doesn't exist Common subsequence , return 0
.
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
- for example ,
"ace"
yes"abcde"
The subsequence , but"aec"
No"abcde"
The subsequence .
Two strings of Common subsequence Is the subsequence shared by these two strings .
Example 1:
Input :text1 = "abcde", text2 = "ace"
Output :3
explain : The longest common subsequence is "ace" , Its length is 3 .
Example 2:
Input :text1 = "abc", text2 = "abc"
Output :3
explain : The longest common subsequence is "abc" , Its length is 3 .
Example 3:
Input :text1 = "abc", text2 = "def"
Output :0
explain : The two strings have no common subsequence , return 0 .
Tips :
1 <= text1.length, text2.length <= 1000
text1
andtext2
It only consists of lowercase English characters .
Answer key
This question is also a typical one dp subject , If you have done the longest ascending subsequence, etc , It is helpful to understand and solve this problem . Let's talk about the longest common subsequence (LCS:Longest Common Subsequence) Well .
Subsequence and substring are different , Substrings must be continuous , Subsequences only need to be in the same order , But there is no guarantee of continuity .
The longest common subsequence is , In two strings , Can we find the longest continuous or discontinuous subsequence , If we don't understand dynamic planning practices , In fact, it can also be brutally cracked , Is to find all subsequences of two strings , If t1 The length is n,t2 The length is m, that t1 There are subsequences of 2n individual ,t2 There are subsequences of 2m individual , The time complexity is as high as 2^(n+m).
That certainly can't consider brute force cracking , How does dynamic programming solve the problem .
We have to use a two-dimensional auxiliary array to record the length of the longest common subsequence ,dp[i] [j] The length of the record is i And length j The longest common subsequence length of the string of . With auxiliary arrays , Then we need to find the dynamic programming transition equation according to the state .
If a string length is 0 The empty string of , The length of another string cannot have a common subsequence , namely dp[i] [0] = dp[0] [j] = 0
If t1[i] = t2[j], that dp[i] [j] = dp[i - 1] [j - 1] + 1, because dp[i - 1] [j - 1] The record is t1[0: i - 1] and t2[0: j - 1] The longest common subsequence length of , If t1[i] = t2[j] Words , Add one to the length .
for example acef and def, When variable to ace and de yes , The longest common subsequence length is 1, Only the last e identical , Then join f after , The longest common subsequence length is 2, namely 1(dp[i - 1] [j -1]) + 1
If t1[i] ≠ t2[j],dp[i] [j] What should it be . We should think about dp[i] [j] What does it mean ,dp[i] [j] Said is t1[0: i] and t2[0: j] The longest common subsequence length of , Then we should take dp[i] [j - 1] and dp[i - 1] [j] The largest of them .
Why is the biggest one of the two , because dp[i] [j - 1] representative t1[i] and t2[j-1],dp[i - 1] [j] representative t1[i-1] and t2[j], These two states include t1[i] ≠ t2[j] The longest possible common subsequence of the .
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int len1 = text1.length();// length 1
int len2 = text2.length();// length 2
int[][] dp = new int[len1 + 1][len2 + 1];// The dynamic array
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
if(text1.charAt(i - 1) == text2.charAt(j - 1)){
//t1[i] = t2[j], that dp[i] [j] = dp[i - 1] [j - 1] + 1
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
//t1[i] ≠ t2[j], Then take dp[i] [j - 1] and dp[i - 1] [j] The largest of them
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[len1][len2];
}
}
边栏推荐
- Agile development practice summary-4
- 删除软链接
- 20220524 how to install coppeliasim to disk D
- 线上调试工具Arthas基础
- Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
- 图数据库Neo4j介绍
- JUC field Updater
- JUC原子引用与ABA问题
- Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
- LeetCode 6096. Success logarithm of spells and potions (binary search)
猜你喜欢
QT multithreaded TCP server
Jenkins接入Openldap用戶認證
C/S模型与P2P模型
Qvector shallow copy performance test
turtle库显示系统时间
线上调试工具Arthas基础
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
turtle库的使用数字时钟模拟时钟动态显示
JUC原子引用与ABA问题
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
随机推荐
C language: Simulated Implementation of library function strcpy
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
JUC 原子累加器
20211006 integral, differential and projection belong to linear transformation
C language: Address Book
Online debugging tool Arthas advanced
Summary of the first retrospective meeting
攻防世界PWN play 条件竞争漏洞的利用
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
Talking about acid of database
C language 7-13 day K candle chart (15 points)
C#入门系列(十三) -- 初识结构体
20211005 Hermite matrix and some properties
20220606 Young's inequality for Matrices
C language: deep understanding of pointers and arrays
CAS无锁
虚拟化和云计算文章大合集
Exporting MySQL data table documents using Navicat
Overview of common layers of image recognition neural network (under update)
Dpdk timer learning notes