题目
与583. 两个字符串的删除操作题目几乎一模一样!
法1:DP
class Solution {
public int minimumDeleteSum(String s1, String s2) {
int m = s1.length() + 1, n = s2.length() + 1;
int[][] dp = new int[m][n];
for (int i = 1; i < n; ++i) { // 首行
dp[0][i] = s2.charAt(i - 1) + dp[0][i - 1];
}
for (int i = 1; i < m; ++i) { // 首列
dp[i][0] = s1.charAt(i - 1) + dp[i - 1][0];
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j] + s1.charAt(i - 1), dp[i][j - 1] + s2.charAt(j - 1));
}
}
}
return dp[m - 1][n - 1];
}
}