LeetCode 1143. Longest Common Subsequence

原题链接在这里:https://leetcode.com/problems/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.

题解:

dp[i][j] stands for length of LCS between text1 up to i and text2 up to j.

If text1.charAt(i) == text2.charAt(j), dp[i][j] = dp[i-1][j-1] + 1.

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

如果不放心的话,就直接取上述三个的最小.

Time Complexity: O(m*n). m = text1.length. n = text2.length.

Space: O(m*n).

AC Java:

 1 class Solution {
 2     public int longestCommonSubsequence(String text1, String text2) {
 3         if(text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0){
 4             return 0;
 5         }
 6
 7         int m = text1.length();
 8         int n = text2.length();
 9         int [][] dp = new int[m+1][n+1];
10         for(int i = 0; i<m; i++){
11             for(int j = 0; j<n; j++){
12                 dp[i+1][j+1] = Math.max(dp[i][j+1], dp[i+1][j]);
13                 if(text1.charAt(i) == text2.charAt(j)){
14                     dp[i+1][j+1] = Math.max(dp[i+1][j+1], dp[i][j]+1);
15                 }
16             }
17         }
18
19         return dp[m][n];
20     }
21 }

类似Longest Palindromic.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11450819.html

时间: 2024-10-06 17:24:08

LeetCode 1143. Longest Common Subsequence的相关文章

【leetcode】1143. Longest Common Subsequence

题目如下: 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

1143. Longest Common Subsequence

link to problem Description: 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 chan

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

uva10405 - Longest Common Subsequence(LIS,最长共同自序列)

题目:uva10405 - Longest Common Subsequence(LIS,最长共同自序列) 题目大意:找出两个字符串中的最长公共的子序列. 解题思路:这类问题是第一次接触,不知道怎么做.百度了一下,发现了递推公式:dp[i][j]:代表第一个字符串的前i个字符和第二个字符串的前j个字符比较能得到的最长的公共子序列.s[i] == s[j] ,dp[i][j] = dp[i - 1][j - 1] + 1: s[i] != s[j] , dp[i][j] = Max (dp[i][

LeetCode 014 Longest Common Prefix

[题目] Write a function to find the longest common prefix string amongst an array of strings. [题意] 求一组字符串的最长公共前缀 [思路] 使用归并思想求解 要求字符串[1,2,..,k,k+1,...n]的最大公共前缀,先分别求[1,2,...k]和[k+1,...,n]的公共前缀 [代码] class Solution { public: string commonPrefix(vector<stri

[HackerRank] The Longest Common Subsequence

This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here. My accepted code is as follows. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5

[Algorithms] Longest Common Subsequence

The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t. Do you know the difference between substring and subequence? Well, substring i

HDU 2253 Longest Common Subsequence Again

其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string.h> #define M 30005 #define SIZE 128 #define WORDMAX 3200 #define BIT 32 char s1[M], s2[M]; int nword; unsigned int str[SIZE][WORDMAX]; unsigned int tmp1