Skip to content

Commit a13f33c

Browse files
[N-0] add 712
1 parent b3dcf00 commit a13f33c

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Your ideas/fixes/algorithms are more than welcome!
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_714.java) | O(n) | O(1) | Medium | DP
2626
|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) | O(n) | O(1) | Medium |
27+
|712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | O(m*n) | O(m*n) | Medium | DP
2728
|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | O(n*(2^n)) | O(2^n) | Medium | Backtracking
2829
|697|[Degree of an Array](https://leetcode.com/problems/degree-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_697.java) | O(n) | O(n) | Easy |
2930
|696|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_696.java) | O(n) | O(n) | Easy |
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 712. Minimum ASCII Delete Sum for Two Strings
5+
*
6+
* Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.
7+
8+
Example 1:
9+
Input: s1 = "sea", s2 = "eat"
10+
Output: 231
11+
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
12+
Deleting "t" from "eat" adds 116 to the sum.
13+
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
14+
15+
Example 2:
16+
Input: s1 = "delete", s2 = "leet"
17+
Output: 403
18+
Explanation: Deleting "dee" from "delete" to turn the string into "let",
19+
adds 100[d]+101[e]+101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum.
20+
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
21+
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
22+
23+
Note:
24+
0 < s1.length, s2.length <= 1000.
25+
All elements of each string will have an ASCII value in [97, 122].
26+
*/
27+
public class _712 {
28+
public static class Solution1 {
29+
//credit: https://leetcode.com/articles/minimum-ascii-delete-sum-for-two-strings/
30+
public int minimumDeleteSum(String s1, String s2) {
31+
int[][] dp = new int[s1.length()+1][s2.length()+1];
32+
33+
for (int i = s1.length()-1; i >= 0; i--) {
34+
dp[i][s2.length()] = dp[i+1][s2.length()] + s1.codePointAt(i);
35+
}
36+
37+
for (int j = s2.length()-1; j >= 0; j--) {
38+
dp[s1.length()][j] = dp[s1.length()][j+1] + s2.codePointAt(j);
39+
}
40+
41+
for (int i = s1.length() - 1; i >= 0; i--) {
42+
for (int j = s2.length()-1; j >= 0; j--) {
43+
if (s1.charAt(i) == s2.charAt(j)) {
44+
dp[i][j] = dp[i+1][j+1];
45+
} else {
46+
dp[i][j] = Math.min(dp[i+1][j] + s1.codePointAt(i), dp[i][j+1] + s2.codePointAt(j));
47+
}
48+
}
49+
}
50+
51+
return dp[0][0];
52+
}
53+
}
54+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._712;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _712Test {
10+
private static _712.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _712.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(231, solution1.minimumDeleteSum("sea", "eat"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(403, solution1.minimumDeleteSum("delete", "leet"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)