Daily LeetCode 1143. Longest Common Subsequence
https://leetcode.com/problems/longest-common-subsequence/submissions/
Medium
问题描述:
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.
Example 1:
1 | Input: text1 = "abcde", text2 = "ace" |
Example 2:
1 | Input: text1 = "abc", text2 = "abc" |
Example 3:
1 | Input: text1 = "abc", text2 = "def" |
Constraints:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
- The input strings consist of lowercase English characters only.
题目分析:
这一题是要找出两个字符串之间的最长公共子串。
思路一:
分别找出两个字符串的子串,然后找出最长的公共子串即可,这种暴力解法在思路上很直接,但是求子串的过程会耗费大量的时间和空间,因此这种方法并不适合。
思路二:
这题其实是一道典型的用动态规划解决的题目,在解决两个字符串相关的问题时,通常需要维护一个二维数组,来描述两个字符串之间的关系。
我们初始化一个dp数组,dp[i][j]
表示当前情况下最长的公共子串长度,即text1
前i
个元素与text2
前j
个元素能构成的最长公共子串长度(i,j
不包括在内)。
接着我们来考虑状态转移方程,更新dp[i][j]
时应当考虑两种情况:第一种是当前遍历的字符相等的情况,即text1[i]==text2[j]
,此时,dp[i][j]=dp[i-1][j-1]+1
,即text1
前i-1
个元素与text2
前j-1
个元素能构成的最长公共子串长度加1;第二种情况是当前遍历的字符不想等的情况,由于字符不等,因此我们只能在dp[i-1][j], dp[i][j-1]
中选一个最大值,因为我们此时不能扩展公共子串了,此时,dp[i][j]=max(dp[i-1][j], dp[i][j-1])
。
以Example1为例:
1 | current text1 index: 1, current text2 index: 1, equal |
代码:
1 | class Solution: |