1143. 最长公共子序列
https://leetcode-cn.com/problems/longest-common-subsequence/
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。
两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。示例 1:
输入:text1 = "abcde", text2 = "ace"
输出:3
解释:最长公共子序列是 "ace" ,它的长度为 3 。
示例 2:输入:text1 = "abc", text2 = "abc"
输出:3
解释:最长公共子序列是 "abc" ,它的长度为 3 。
示例 3:输入:text1 = "abc", text2 = "def"
输出:0
解释:两个字符串没有公共子序列,返回 0 。提示:
1 <= text1.length, text2.length <= 1000
text1
和text2
仅由小写英文字符组成。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.size() + 1, vector<int>(text2.size() + 1, 0));
for (int i = 1; i <= text1.size(); i++) {
for (int j = 1; j <= text2.size(); 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[text1.size()][text2.size()];
}
};
描述
给定两个字符串str1和str2,输出两个字符串的最长公共子序列。如果最长公共子序列为空,则返回"-1"。目前给出的数据,仅仅会存在一个最长的公共子序列
数据范围:0 \le |str1|,|str2| \le 20000≤∣str1∣,∣str2∣≤2000
要求:空间复杂度 O(n^2)O(n2) ,时间复杂度 O(n^2)O(n2)
示例1
输入:"1A2C3D4B56","B1D23A456A" 返回值:"123456"
示例2
输入:"abc","def" 返回值:"-1"
示例3
输入:"abc","abc" 返回值:"abc"
示例4
输入:"ab",""返回值:"-1"
/**
*上面的题目的一种变形 动规使用字符串
*/
import java.util.*;
public class Solution {
/**
* longest common subsequence
* @param s1 string字符串 the string
* @param s2 string字符串 the string
* @return string字符串
*/
public String LCS (String s1, String s2) {
int len1 = s1.length(), len2 = s2.length();
// 明确状态: 当前需要处理的s1和s2分别前i和前j个元素
// dp[i][j]表示从左到右,当处理到s1的第i个元素和s2的第j个元素时的公共子序列
String[][] dp = new String[len1 + 1][len2 + 1];
// base case:当i==0或j==0的情况,dp[i][j]为"",因为空字符串没有公共子序列
for(int i = 0; i <= len1; i++) {
// j == 0
dp[i][0] = "";
}
for(int j = 0; j <= len2; j++) {
// i == 0
dp[0][j] = "";
}
// 状态转移
for(int i = 1; i <= len1; i++) {
for(int j = 1; j <= len2; j++) {
// 当前字符相等,则添加结果
if(s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i-1][j-1] + s1.charAt(i-1);
} else {
// 当前字符不相等,则还需要分两种情况,取长度较长的情况
dp[i][j] = dp[i-1][j].length() > dp[i][j-1].length() ? dp[i-1][j] : dp[i][j-1];
}
}
}
return dp[len1][len2] == "" ? "-1" : dp[len1][len2];
}
}
描述
给定两个字符串str1和str2,输出两个字符串的最长公共子序列。如果最长公共子序列为空,则返回"-1"。目前给出的数据,仅仅会存在一个最长的公共子序列
数据范围:0 \le |str1|,|str2| \le 20000≤∣str1∣,∣str2∣≤2000
要求:空间复杂度 O(n^2)O(n2) ,时间复杂度 O(n^2)O(n2)
示例1
输入:"1A2C3D4B56","B1D23A456A" 返回值:"123456"
与前面的区别在于 他求的是连续的最长字串
import java.util.*;
public class Solution {
/**
* longest common substring
* @param str1 string字符串 the string
* @param str2 string字符串 the string
* @return string字符串
*/
public String LCS (String str1, String str2) {
// write code here
int maxLenth = 0;//记录最长公共子串的长度
//记录最长公共子串最后一个元素在字符串str1中的位置
int maxLastIndex = 0;
int[][] dp = new int[str1.length() + 1][str2.length() + 1];
for(int i=1;i<=str1.length();i++){
for(int j=1;j<=str2.length();j++){
if(str1.charAt(i-1)==str2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1]+1;
if(dp[i][j]>maxLenth){
maxLenth = dp[i][j];
maxLastIndex = i-1;
}
}else{
//递推公式,两个字符不相等的情况
dp[i][j] = 0;
}
}
}
//最字符串进行截取,substring(a,b)中a和b分别表示截取的开始和结束位置
return str1.substring(maxLastIndex - maxLenth+1, maxLastIndex+1);
}
}