##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/712.html

# 712. Minimum ASCII Delete Sum for Two Strings (Medium)

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].
• Companies:
TripleByte

Related Topics:
Dynamic Programming

Similar Questions:

## Solution 1. DP

This question is very similar to 72. Edit Distance (Hard).

Let dp[i][j] be the result for S[0..(i-1)] and T[0..(j-1)].

• If S[i - 1] == T[j - 1], dp[i][j] = dp[i - 1][j - 1], meaning we simply reuse the result dp[i - 1][j - 1].
• If S[i - 1] != T[j - 1], we pick the minimum from the following two options:
• S[i - 1] + dp[i - 1][j], meaning delete S[i - 1] and reuse the result dp[i - 1][j].
• T[j - 1] + dp[i][j - 1], meaning delete T[j - 1] and reuse the result dp[i][j - 1].
dp[i][j] = dp[i - 1][j - 1]                                           if S[i - 1] == T[j - 1]
= min(S[i - 1] + dp[i - 1][j], T[j - 1] + dp[i][j - 1])      if S[i - 1] != T[j - 1]

dp[i][0] = S[0] + ... + S[i - 1]         (0 < i <= M)
dp[0][j] = T[0] + ... + T[j - 1]         (0 < j <= N)

// OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int M = s1.size(), N = s2.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1));
for (int i = 1; i <= M; ++i) dp[i][0] = dp[i - 1][0] + s1[i - 1];
for (int i = 1; i <= N; ++i) dp[0][i] = dp[0][i - 1] + s2[i - 1];
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min(s1[i - 1] + dp[i - 1][j], s2[j - 1] + dp[i][j - 1]);
}
}
return dp[M][N];
}
};


Or in another form:

// OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int minimumDeleteSum(string s, string t) {
int M = s.size(), N = t.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, 1e9));
for (int i = 0; i <= M; ++i) {
for (int j = 0; j <= N; ++j) {
if (!i && !j) dp[i][j] = 0;
else if (i && j && s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min(i ? dp[i - 1][j] + s[i - 1] : 1e9, j ? dp[i][j - 1] + t[j - 1] : 1e9);
}
}
return dp[M][N];
}
};


## Solution 2. DP with Space Optimization

Use rolling array to optimize the space to 2 * min(M, N).

// OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings
// Time: O(MN)
// Space: O(min(M, N))
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
if (s1.size() > s2.size()) swap(s1, s2);
int M = s1.size(), N = s2.size();
vector<vector<int>> dp(2, vector<int>(N + 1));
for (int i = 1; i <= N; ++i) dp[0][i] = dp[0][i - 1] + s2[i - 1];
for (int i = 1; i <= M; ++i) {
dp[i % 2][0] = dp[(i - 1) % 2][0] + s1[i - 1];
for (int j = 1; j <= N; ++j) {
if (s1[i - 1] == s2[j - 1]) dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
else dp[i % 2][j] = min(s1[i - 1] + dp[(i - 1) % 2][j], s2[j - 1] + dp[i % 2][j - 1]);
}
}
return dp[M % 2][N];
}
};


## Solution 3. DP with Further Space Optimization

One thing that prevents us from using one dimensional array is the dependency between dp[i + 1][j + 1] and dp[i][j] since when we visit dp[i + 1][j + 1], dp[i][j] is overwritten by dp[i + 1][j]. We can store dp[i][j] in a temporary variable.

// OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings
// Time: O(MN)
// Space: O(min(M, N))
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
if (s1.size() > s2.size()) swap(s1, s2);
int M = s1.size(), N = s2.size();
vector<int> dp(N + 1);
for (int i = 1; i <= N; ++i) dp[i] = dp[i - 1] + s2[i - 1];
for (int i = 1; i <= M; ++i) {
int prev = dp[0];
dp[0] += s1[i - 1];
for (int j = 1; j <= N; ++j) {
int next = dp[j];
if (s1[i - 1] == s2[j - 1]) dp[j] = prev;
else dp[j] = min(s1[i - 1] + dp[j], s2[j - 1] + dp[j - 1]);
prev = next;
}
}
return dp[N];
}
};


Or:

// OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// Time: O(MN)
// Space: O(min(M, N))
class Solution {
public:
int minimumDeleteSum(string s, string t) {
int M = s.size(), N = t.size();
if (M < N) swap(M, N), swap(s, t);
vector<int> dp(N + 1, 1e9);
for (int i = 0; i <= M; ++i) {
int prev;
for (int j = 0; j <= N; ++j) {
int cur = dp[j];
if (!i && !j) dp[j] = 0;
else if (i && j && s[i - 1] == t[j - 1]) dp[j] = prev;
else dp[j] = min(i ? dp[j] + s[i - 1] : 1e9, j ? dp[j - 1] + t[j - 1] : 1e9);
prev = cur;
}
}
return dp[N];
}
};

• class Solution {
public int minimumDeleteSum(String s1, String s2) {
int length1 = s1.length(), length2 = s2.length();
int[][] dp = new int[length1 + 1][length2 + 1];
for (int i = length1 - 1; i >= 0; i--)
dp[i][length2] = dp[i + 1][length2] + s1.codePointAt(i);
for (int i = length2 - 1; i >= 0; i--)
dp[length1][i] = dp[length1][i + 1] + s2.codePointAt(i);
for (int i = length1 - 1; i >= 0; i--) {
for (int j = length2 - 1; j >= 0; j--) {
if (s1.codePointAt(i) == s2.codePointAt(j))
dp[i][j] = dp[i + 1][j + 1];
else
dp[i][j] = Math.min(dp[i + 1][j] + s1.codePointAt(i), dp[i][j + 1] + s2.codePointAt(j));
}
}
return dp[0][0];
}
}

############

class Solution {
public int minimumDeleteSum(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1.charAt(i - 1);
}
for (int j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2.charAt(j - 1);
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j]
= Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
}
}
}
return f[m][n];
}
}

• // OJ: https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int M = s1.size(), N = s2.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1));
for (int i = 1; i <= M; ++i) dp[i][0] = dp[i - 1][0] + s1[i - 1];
for (int i = 1; i <= N; ++i) dp[0][i] = dp[0][i - 1] + s2[i - 1];
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min(s1[i - 1] + dp[i - 1][j], s2[j - 1] + dp[i][j - 1]);
}
}
return dp[M][N];
}
};

• class Solution:
def minimumDeleteSum(self, s1: str, s2: str) -> int:
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
dp[i][0] = dp[i - 1][0] + ord(s1[i - 1])
for j in range(1, n + 1):
dp[0][j] = dp[0][j - 1] + ord(s2[j - 1])
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(
dp[i - 1][j] + ord(s1[i - 1]), dp[i][j - 1] + ord(s2[j - 1])
)
return dp[-1][-1]

############

class Solution(object):
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
l1, l2 = len(s1), len(s2)
dp = [[0] * (l2 + 1) for _ in range(l1 + 1)]
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + ord(s1[i - 1])
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
result = sum(map(ord, s1 + s2)) - dp[-1][-1] * 2
return result

• func minimumDeleteSum(s1 string, s2 string) int {
m, n := len(s1), len(s2)
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for i, c := range s1 {
f[i+1][0] = f[i][0] + int(c)
}
for j, c := range s2 {
f[0][j+1] = f[0][j] + int(c)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if s1[i-1] == s2[j-1] {
f[i][j] = f[i-1][j-1]
} else {
f[i][j] = min(f[i-1][j]+int(s1[i-1]), f[i][j-1]+int(s2[j-1]))
}
}
}
return f[m][n]
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

• function minimumDeleteSum(s1: string, s2: string): number {
const m = s1.length;
const n = s2.length;
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1[i - 1].charCodeAt(0);
}
for (let j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2[j - 1].charCodeAt(0);
}
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
if (s1[i - 1] === s2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = Math.min(
f[i - 1][j] + s1[i - 1].charCodeAt(0),
f[i][j - 1] + s2[j - 1].charCodeAt(0),
);
}
}
}
return f[m][n];
}


• /**
* @param {string} s1
* @param {string} s2
* @return {number}
*/
var minimumDeleteSum = function (s1, s2) {
const m = s1.length;
const n = s2.length;
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1[i - 1].charCodeAt(0);
}
for (let j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2[j - 1].charCodeAt(0);
}
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
if (s1[i - 1] === s2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = Math.min(
f[i - 1][j] + s1[i - 1].charCodeAt(0),
f[i][j - 1] + s2[j - 1].charCodeAt(0),
);
}
}
}
return f[m][n];
};