[LeetCode] 1143. Longest Common Subsequence

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 and text2 consist of only lowercase English characters.

最长公共子序列。

给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。

例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。
两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-common-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

首先创建DP数组,dp[i][j] 的含义是以 text1 在位置 i 和 text2 在位置 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 题目总结

posted @ 2020-07-01 03:55  CNoodle  阅读(225)  评论(0编辑  收藏  举报