zoukankan      html  css  js  c++  java
  • [LeetCode] 1143. Longest Common Subsequence

    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.

    最长公共子序列。这个题可以跟718题一起刷,基本是一样的意思,思路都是动态规划的背包问题。

    首先创建DP数组,DP[i][j]的含义是以i和j为结尾的最长的公共子序列的长度。初始化的时候dp[0][0] = 0

    如果当前的字母相同,text1.charAt(i - 1) == text2.charAt(j - 1),那么dp[i][j] = dp[i - 1][j - 1] + 1

    否则,dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]),

    此时最长公共子序列为 text1 的前 i - 1 个字符和 text2 的前 j 个字符最长公共子序列,或者 text1 的前 i 个字符和 text2 的前 j - 1 个字符最长公共子序列,取它们之间较大的一个

    时间O(mn)

    空间O(mn)

    Java实现

     1 class Solution {
     2     public int longestCommonSubsequence(String text1, String text2) {
     3         int m = text1.length();
     4         int n = text2.length();
     5         int[][] dp = new int[m + 1][n + 1];
     6         for (int i = 1; i <= m; i++) {
     7             for (int j = 1; j <= n; j++) {
     8                 dp[i][j] = text1.charAt(i - 1) == text2.charAt(j - 1) ? dp[i - 1][j - 1] + 1
     9                         : Math.max(dp[i - 1][j], dp[i][j - 1]);
    10             }
    11         }
    12         return dp[m][n];
    13     }
    14 }

    JavaScript实现

     1 /**
     2  * @param {string} text1
     3  * @param {string} text2
     4  * @return {number}
     5  */
     6 var longestCommonSubsequence = function (text1, text2) {
     7     let m = text1.length;
     8     let n = text2.length;
     9     let dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
    10     for (let i = 1; i <= m; i++) {
    11         for (let j = 1; j <= n; j++) {
    12             dp[i][j] =
    13                 text1[i - 1] == text2[j - 1]
    14                     ? dp[i - 1][j - 1] + 1
    15                     : Math.max(dp[i - 1][j], dp[i][j - 1]);
    16         }
    17     }
    18     return dp[m][n];
    19 };

    相关题目

    718. Maximum Length of Repeated Subarray

    1143. Longest Common Subsequence

    LeetCode 题目总结

  • 相关阅读:
    nyoj163 Phone List
    hdu1251统计难题
    hdu1754 I Hate It
    nyoj123 士兵杀敌(四)
    poj3468 A Simple Problem with Integers
    zoj1610 Count the Colors
    nyoj144 小珂的苦恼
    nyoj93 汉诺塔(三)
    poj2182 Lost Cows
    ASP.NET2.0中的Callback机制
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13217137.html
Copyright © 2011-2022 走看看