Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 1143. Longest Common Subsequence #1143

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 1143. Longest Common Subsequence #1143

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

Given two strings text1 and text2, return the length of their longest common subsequence.

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:

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 <= 1000
  • 1 <= text2.length <= 1000
  • The input strings consist of lowercase English characters only.

这道题让求最长相同的子序列,注意是子序列,不是子串,所以字符并不需要相连,但是字符顺序还是需要保持的。LeetCode 之前也有题目需要借助求 LCS 来解题,比如 Delete Operation for Two Strings,当时博主还疑惑怎么 LeetCode 中没有专门求 LCS 的题呢,这不,终于补上了。解题思路和上面那道是一模一样,若搞懂了那道题,这道也就是没什么难度了,这里是用动态规划 Dynamic Programing 来做,使用一个二维数组 dp,其中 dp[i][j] 表示 text1 的前i个字符和 text2 的前j个字符的最长相同的子序列的字符个数,这里大小初始化为 (m+1)x(n+1),这里的m和n分别是 text1 和 text2 的长度。接下来就要找状态转移方程了,如何来更新 dp[i][j],若二者对应位置的字符相同,表示当前的 LCS 又增加了一位,所以可以用 dp[i-1][j-1] + 1 来更新 dp[i][j]。否则若对应位置的字符不相同,由于是子序列,还可以错位比较,可以分别从 text1 或者 text2 去掉一个当前字符,那么其 dp 值就是 dp[i-1][j] 和 dp[i][j-1],取二者中的较大值来更新 dp[i][j] 即可,最终的结果保存在了 dp[m][n] 中,参见代码如下:

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int m = text1.size(), n = text2.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (text1[i - 1] == text2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[m][n];
    }
};

讨论:这道题跟之前那道 Longest Palindromic Subsequence 的解题思路几乎是一模一样,你品,你细品,一定要通过现象看本质,才能举一反三,触类旁通。

Github 同步地址:

#1143

类似题目:

Longest Palindromic Subsequence

Delete Operation for Two Strings

Shortest Common Supersequence

参考资料:

https://leetcode.com/problems/longest-common-subsequence/

https://leetcode.com/problems/longest-common-subsequence/discuss/348884/C%2B%2B-with-picture-O(nm)

https://leetcode.com/problems/longest-common-subsequence/discuss/351689/JavaPython-3-Two-DP-codes-of-O(mn)-and-O(min(m-n))-spaces-w-picture-and-analysis

LeetCode All in One 题目讲解汇总(持续更新中...)

@grandyang grandyang changed the title [LeetCode] 1143. Missing Problem [LeetCode] 1143. Longest Common Subsequence Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant