Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 878ebd0

Browse files
committed
Add solution #712
1 parent eec82f3 commit 878ebd0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 712. Minimum ASCII Delete Sum for Two Strings
3+
* https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
4+
* Difficulty: Medium
5+
*
6+
* Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to
7+
* make two strings equal.
8+
*/
9+
10+
/**
11+
* @param {string} s1
12+
* @param {string} s2
13+
* @return {number}
14+
*/
15+
var minimumDeleteSum = function(s1, s2) {
16+
const dp = new Array(s1.length + 1).fill().map(() => {
17+
return new Array(s2.length + 1).fill(0);
18+
});
19+
20+
for (let i = 1; i <= s1.length; i++) {
21+
dp[i][0] = dp[i - 1][0] + s1.charCodeAt(i - 1);
22+
}
23+
for (let j = 1; j <= s2.length; j++) {
24+
dp[0][j] = dp[0][j - 1] + s2.charCodeAt(j - 1);
25+
}
26+
27+
for (let i = 1; i <= s1.length; i++) {
28+
for (let j = 1; j <= s2.length; j++) {
29+
if (s1[i - 1] === s2[j - 1]) {
30+
dp[i][j] = dp[i - 1][j - 1];
31+
} else {
32+
dp[i][j] = Math.min(
33+
dp[i - 1][j] + s1.charCodeAt(i - 1),
34+
dp[i][j - 1] + s2.charCodeAt(j - 1)
35+
);
36+
}
37+
}
38+
}
39+
40+
return dp[s1.length][s2.length];
41+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@
537537
706|[Design HashMap](./0706-design-hashmap.js)|Easy|
538538
707|[Design Linked List](./0707-design-linked-list.js)|Medium|
539539
710|[Random Pick with Blacklist](./0710-random-pick-with-blacklist.js)|Hard|
540+
712|[Minimum ASCII Delete Sum for Two Strings](./0712-minimum-ascii-delete-sum-for-two-strings.js)|Medium|
540541
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
541542
714|[Best Time to Buy and Sell Stock with Transaction Fee](./0714-best-time-to-buy-and-sell-stock-with-transaction-fee.js)|Medium|
542543
717|[1-bit and 2-bit Characters](./0717-1-bit-and-2-bit-characters.js)|Easy|

0 commit comments

Comments
 (0)