Если даны две строки text1 и text2, верните длину их самой длинной общей подпоследовательности. Если общей подпоследовательности нет, верните 0.
Подпоследовательность строки — это новая строка, созданная из исходной строки с удалением некоторых символов (их может не быть) без изменения относительного порядка оставшихся символов.
Например, «ace» является подпоследовательностью «abcde».
Общая подпоследовательность двух строк — это подпоследовательность, которая является общей для обеих строк.
Пример 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Решение:
Подход снизу вверх (мемоизация) :
Временная сложность: O(m*n)
, где m
и n
— длина двух строк a
и b
.
пространственная сложность : o(m*n) для использования 2d dp
и O(m+n)
для вспомогательного стекового пространства, поскольку в худшем случае мы будем делать m+n
рекурсивных вызовов.
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
// we will start with the last index if its a match then we will decrement both the index
//else we will decrement text1 index keeping text2 index same and in second method call we will decrement text2 index keeping the text1 index same
// by this we will cover all the possibility
// and we will be able to get substring with the largest length common in both the Strings
// lets optimize with dp
int dp[][] = new int[text1.length()][text2.length()];
for(int row[]: dp) Arrays.fill(row,-1);
return findLcsLength(text1,text2,text1.length()-1,text2.length()-1,dp);
}
public int findLcsLength(String a, String b, int i,int j,int dp[][]){
if(i<0 || j<0) return 0;
if(dp[i][j]!=-1) return dp[i][j];
if(a.charAt(i) ==b.charAt(j)){
return dp[i][j] = 1 + findLcsLength(a,b,i-1,j-1,dp);
}
else {
return dp[i][j]= 0+Integer.max(findLcsLength(a,b,i-1,j,dp),findLcsLength(a,b,i,j-1,dp));
}
}
}
Табуляция
class Solution {
public int longestCommonSubsequence(String str1, String str2) {
int dp[][] = new int[str1.length()+1][str2.length()+1];
for(int i=0;i<=str1.length();i++){
dp[i][0] =0;
}
for( int i =0;i<=str2.length();i++){
dp[0][i] = 0;
}
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] = 1 + dp[i-1][j-1];
}
else dp[i][j] = Integer.max(dp[i][j-1],dp[i-1][j]);
}
}
return dp[str1.length()][str2.length()];
}
}
Если нам нужно вывести строку с самой длинной общей подпоследовательностью, просто добавьте следующее в вышеприведенный код.
int p = str1.length(), q = str2.length();
while(p>0 && q>0){
if(str1.charAt(p-1) == str2.charAt(q-1)){
str = str1.charAt(p-1)+ str;
p--;
q--;
}
else if(dp[p][q-1] > dp[p-1][q]){
q--;
}
else p--;
}
System.out.println(str);