题目

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.


Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum.  Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.


Note:

• 0 < s1.length, s2.length <= 1000.
• All elements of each string will have an ASCII value in [97, 122].

暴力回溯算法

代码

class Solution {
public int minimumDeleteSum(String s1, String s2) {
Map<String, Integer> shorter = new HashMap<>();
Map<String, Integer> longer = new HashMap<>();
if (s1.length() >= s2.length()) {
longer.put(s1, 0);
shorter.put(s2, 0);
longer = removeNChar(s1.length() - s2.length(), longer);
} else {
longer.put(s2, 0);
shorter.put(s1, 0);
longer = removeNChar(s2.length() - s1.length(), longer);
}
int min = Math.min(s1.length(), s2.length());
boolean matched = false;
int res = Integer.MAX_VALUE;
for (int i = 0; i <= min; i++) {
for (Map.Entry<String, Integer> entry : shorter.entrySet()) {
if (longer.containsKey(entry.getKey())) {
res = Math.min(res, entry.getValue() + longer.get(entry.getKey()));
matched = true;
}
}
if (matched) return res;
shorter = removeOneChar(shorter);
longer = removeOneChar(longer);
}
return 0; // never reached
}
private Map<String, Integer> removeNChar(int n, Map<String, Integer> table) {
for (int i = 0; i < n; i++) {
table = removeOneChar(table);
}
return table;
}
// backtracking
private Map<String, Integer> removeOneChar(Map<String, Integer> table) {
Map<String, Integer> newTable = new HashMap<>();
for (Map.Entry<String, Integer> entry : table.entrySet()) {
StringBuilder sb = new StringBuilder(entry.getKey());
int sum = entry.getValue();
for (int i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
sb.delete(i, i + 1);
if (!newTable.containsKey(sb.toString())) {
newTable.put(sb.toString(), sum + (int) c);
}
sb.insert(i, c);
}
}
return newTable;
}
}


最短编辑距离（动态规划）

1. dp[i - 1][j] + s1[i]（图中黄色）：这一列竖直对位下来，前面这个d和空字符_对位所以被删掉了，现在多来了一个l，也要删掉，所以删两个d + l
2. dp[i][j - 1] + s2[j]（图中蓝色）：这一行横向对位过来，前面的l和空字符_对位被删掉，现在又来了一个d，所以也删掉，就删两个l + d
3. dp[i - 1][j - 1] + x（图中紫色）：又要分两种情况： 1）s1[i] == s2[j]时，相当于当前两位对上了，都不删除。所以继承左斜上角的子问题解。 2）s1[i] != s2[j]时，就是图中的情况，l != d，就是在左斜上角的基础上再删除当前来的两位。因为左斜上角子问题都是空字符，所以就删除l + d

代码

class Solution {
public int minimumDeleteSum(String s1, String s2) {
int[][] dp = new int[s1.length() + 1][s2.length() + 1];
for (int i = 1; i <= s1.length(); i++) {
dp[i][0] = dp[i - 1][0] + (int) s1.charAt(i - 1);
}
for (int j = 1; j <= s2.length(); j++) {
dp[0][j] = dp[0][j - 1] + (int) s2.charAt(j - 1);
}
for (int i = 1; i <= s1.length(); i++) {
char cRow = s1.charAt(i - 1);
for (int j = 1; j <= s2.length(); j++) {
char cCol = s2.charAt(j - 1);
int fromUpper = dp[i - 1][j] + cRow;
int fromLeft = dp[i][j - 1] + cCol;
dp[i][j] = Math.min(fromUpper, fromLeft);
int fromBevel = 0;
if (cRow == cCol) {
fromBevel = dp[i - 1][j - 1];
} else {
fromBevel = dp[i - 1][j - 1] + cRow + cCol;
}
dp[i][j] = Math.min(dp[i][j], fromBevel);
}
}
return dp[s1.length()][s2.length()];
}
}


只用两个一维数组

代码

class Solution {
public int minimumDeleteSum(String s1, String s2) {
int[] lineA = new int[s2.length() + 1];
int[] lineB = new int[s2.length() + 1];
for (int i = 1; i <= s2.length(); i++) {
lineA[i] = lineA[i - 1] + s2.charAt(i - 1);
}
for (int i = 1; i <= s1.length(); i++) {
char cRow = s1.charAt(i - 1);
lineB[0] = lineA[0] + cRow;
for (int j = 1; j <= s2.length(); j++) {
char cCol = s2.charAt(j - 1);
int fromUpper = lineA[j] + cRow;
int fromLeft = lineB[j - 1] + cCol;
lineB[j] = Math.min(fromUpper, fromLeft);
int fromBevel = 0;
if (cRow == cCol) {
fromBevel = lineA[j - 1];
} else {
fromBevel = lineA[j - 1] + cRow + cCol;
}
lineB[j] = Math.min(lineB[j], fromBevel);
}
int[] lineTemp = lineA;
lineA = lineB;
lineB = lineTemp;
}
return lineA[s2.length()];
}
}