-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimumASCIIDeleteSumForTwoStrings.java
47 lines (44 loc) · 1.63 KB
/
MinimumASCIIDeleteSumForTwoStrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/*/
class Solution {
public int minimumDeleteSum(String s1, String s2) {
int dp[][] = new int[s1.length()+1][s2.length()+1];
for (int i = dp.length-1; i >= 0; --i)
{
for (int j = dp[0].length-1; j >= 0; --j)
{
if (i == dp.length-1 && j == dp[0].length-1)
dp[i][j] = 0;
else if (i == dp.length-1)
dp[i][j] = (int)s2.charAt(j)+dp[i][j+1];
else if (j == dp[0].length-1)
dp[i][j] = (int)s1.charAt(i)+dp[i+1][j];
else
{
if (s1.charAt(i) == s2.charAt(j))
dp[i][j] = dp[i+1][j+1];
else dp[i][j] = Math.min((int)s1.charAt(i)+dp[i+1][j],(int)s2.charAt(j)+dp[i][j+1]);
}
}
}
return dp[0][0];
}
}
class Solution {
public int minimumDeleteSum(String s1, String s2) {
int dp[][] = new int[s1.length()+1][s2.length()+1];
int i, j, m = s1.length(), n = s2.length();
for (i = m-1; i >= 0; --i)
dp[i][n] = dp[i+1][n]+(int)s1.charAt(i);
for (j = n-1; j >= 0; --j)
dp[m][j] = dp[m][j+1]+(int)s2.charAt(j);
for (i = m-1; i >= 0; --i)
{
for (j = n-1; j >= 0; --j)
{
if (s1.charAt(i) == s2.charAt(j)) dp[i][j] = dp[i+1][j+1];
else dp[i][j] = Math.min(dp[i+1][j]+(int)s1.charAt(i),dp[i][j+1]+(int)s2.charAt(j));
}
}
return dp[0][0];
}
}