1143.最长公共子序列
思路
- dp:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j]
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int size1 = text1.length();
int size2 = text2.length();
int[][] dp = new int[size1 + 1][size2 + 1];
for(int i = 1;i <= size1;i++){
for(int j = 1;j <= size2;j++){
if(text1.charAt(i-1) == text2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[size1][size2];
}
}