Given two strings text1
and text2
, return the length of their longest common subsequence. If there is no common subsequence, return 0
.
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.
- For example,
"ace"
is a subsequence of"abcde"
.
A common subsequence of two strings is a subsequence that is common to both strings.
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, text2.length <= 1000
text1
andtext2
consist of only lowercase English characters.
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。
这一题是经典的最长公共子序列的问题。解题思路是二维动态规划。假设字符串
text1
和text2
的长度分别为m
和n
,创建m+1
行n+1
列的二维数组dp
,定义dp[i][j]
表示长度为 i 的text1[0:i-1]
和长度为 j 的text2[0:j-1]
的最长公共子序列的长度。先考虑边界条件。当i = 0
时,text1[]
为空字符串,它与任何字符串的最长公共子序列的长度都是0
,所以dp[0][j] = 0
。同理当j = 0
时,text2[]
为空字符串,它与任何字符串的最长公共子序列的长度都是0
,所以dp[i][0] = 0
。由于二维数组的大小特意增加了1
,即m+1
和n+1
,并且默认值是0
,所以不需要再初始化赋值了。当
text1[i−1] = text2[j−1]
时,将这两个相同的字符称为公共字符,考虑text1[0:i−1]
和text2[0:j−1]
的最长公共子序列,再增加一个字符(即公共字符)即可得到text1[0:i]
和text2[0:j]
的最长公共子序列,所以dp[i][j]=dp[i−1][j−1]+1
。当text1[i−1] != text2[j−1]
时,最长公共子序列一定在text[0:i-1], text2[0:j]
和text[0:i], text2[0:j-1]
中取得。即dp[i][j] = max(dp[i-1][j], dp[i][j-1])
。所以状态转移方程如下:$$dp[i][j] = \left{\begin{matrix}dp[i-1][j-1]+1 &,text1[i-1]=text2[j-1]\max(dp[i-1][j],dp[i][j-1])&,text1[i-1]\neq text2[j-1]\end{matrix}\right.$$
最终结果存储在
dp[len(text1)][len(text2)]
中。时间复杂度O(mn)
,空间复杂度O(mn)
,其中m
和n
分别是text1
和text2
的长度。
package leetcode funclongestCommonSubsequence(text1string, text2string) int { iflen(text1) ==0||len(text2) ==0 { return0 } dp:=make([][]int, len(text1)+1) fori:=rangedp { dp[i] =make([]int, len(text2)+1) } fori:=1; i<len(text1)+1; i++ { forj:=1; j<len(text2)+1; j++ { iftext1[i-1] ==text2[j-1] { dp[i][j] =dp[i-1][j-1] +1 } else { dp[i][j] =max(dp[i][j-1], dp[i-1][j]) } } } returndp[len(text1)][len(text2)] } funcmax(a, bint) int { ifa>b { returna } returnb }