1143. Longest Common Subsequence #
Problem #
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 <= 1000text1andtext2consist of only lowercase English characters.
Problem Summary #
Given two strings text1 and text2, return the length of the longest common subsequence of these two strings. If there is no common subsequence, return 0. A subsequence of a string is a new string formed from the original string by deleting some characters (or none) without changing the relative order of the remaining characters. For example, “ace” is a subsequence of “abcde”, but “aec” is not a subsequence of “abcde”. A common subsequence of two strings is a subsequence that both strings have in common.
Solution Approach #
This is the classic longest common subsequence problem. The solution approach is two-dimensional dynamic programming. Suppose the lengths of strings
text1andtext2aremandnrespectively. Create a two-dimensional arraydpwithm+1rows andn+1columns, and definedp[i][j]as the length of the longest common subsequence oftext1[0:i-1]with length i andtext2[0:j-1]with length j. First consider the boundary conditions. Wheni = 0,text1[]is an empty string, and the length of its longest common subsequence with any string is0, sodp[0][j] = 0. Similarly, whenj = 0,text2[]is an empty string, and the length of its longest common subsequence with any string is0, sodp[i][0] = 0. Since the size of the two-dimensional array is deliberately increased by1, namelym+1andn+1, and the default value is0, no further initialization assignment is needed.When
\[ 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. \]text1[i−1] = text2[j−1], call these two identical characters the common character. Consider the longest common subsequence oftext1[0:i−1]andtext2[0:j−1]; adding one more character (the common character) gives the longest common subsequence oftext1[0:i]andtext2[0:j], sodp[i][j]=dp[i−1][j−1]+1. Whentext1[i−1] != text2[j−1], the longest common subsequence must be obtained fromtext[0:i-1], text2[0:j]ortext[0:i], text2[0:j-1]. That is,dp[i][j] = max(dp[i-1][j], dp[i][j-1]). Therefore, the state transition equation is as follows:The final result is stored in
dp[len(text1)][len(text2)]. The time complexity isO(mn), and the space complexity isO(mn), wheremandnare the lengths oftext1andtext2respectively.
Code #
package leetcode
func longestCommonSubsequence(text1 string, text2 string) int {
if len(text1) == 0 || len(text2) == 0 {
return 0
}
dp := make([][]int, len(text1)+1)
for i := range dp {
dp[i] = make([]int, len(text2)+1)
}
for i := 1; i < len(text1)+1; i++ {
for j := 1; j < len(text2)+1; j++ {
if text1[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])
}
}
}
return dp[len(text1)][len(text2)]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}