Skip to content

Commit 6c0253e

Browse files
committed
Create 712. 两个字符串的最小ASCII删除和.js
1 parent f2bd083 commit 6c0253e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s1
3+
* @param {string} s2
4+
* @return {number}
5+
*/
6+
var minimumDeleteSum = function (s1, s2) {
7+
const dp = [[]];
8+
for (let i = 0; i <= s1.length; i++) {
9+
dp[i + 1] = [];
10+
for (let j = 0; j <= s2.length; j++) {
11+
if (i === 0 && j === 0) {
12+
dp[i][j] = 0;
13+
} else if (i === 0) {
14+
dp[i][j] = (dp[i][j - 1] || 0) + s2.charCodeAt(j - 1);
15+
} else if (j === 0) {
16+
dp[i][j] = ((dp[i - 1] && dp[i - 1][j]) || 0) + s1.charCodeAt(i - 1);
17+
} else {
18+
if (s1[i - 1] === s2[j - 1]) {
19+
dp[i][j] = dp[i - 1][j - 1];
20+
} else {
21+
dp[i][j] = Math.min(
22+
dp[i - 1][j] + s1.charCodeAt(i - 1),
23+
dp[i][j - 1] + s2.charCodeAt(j - 1),
24+
);
25+
}
26+
}
27+
}
28+
}
29+
return dp[s1.length][s2.length];
30+
};

0 commit comments

Comments
 (0)