Skip to content

712. Minimum ASCII Delete Sum for Two Strings

Problem

Given two strings s1 and s2, return 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.

Constraints:

  • 1 <= s1.length, s2.length <= 1000
  • s1 and s2 consist of lowercase English letters.

Solve

Recursion with cache

This is a good tactic to do any code challenger:

  • To solve any problem with some form of Dynamic programming, we should start finding pattern.
  • By using Recursion, we can try: Reuse the same function, using cache to have some form of reuse result from our last calculation

Here, I found a pattern that, with provided s1 and s2, we can try:

  • Delete one char from s1
  • Delete one char from s2
  • Keep same char both on s1 and s2

To make the problem repeatable, by forcing the character we care about is last char of each string:

class Solution:
    def minimumDeleteSum(self, s1: str, s2: str, cache = None) -> int:
        if cache is None:
            cache = {}

        if (s1, s2) in cache:
            return cache[(s1, s2)]

        if s1 == s2 == "":
            return 0
        possible = []
        if len(s2) > 0:
            possible.append(self.minimumDeleteSum(s1, s2[:len(s2) - 1], cache) + ord(s2[-1]))
        if len(s1) > 0:
            possible.append(self.minimumDeleteSum(s1[:len(s1) - 1], s2, cache) + ord(s1[-1]))
        if len(s2) > 0 and len(s1) > 0 and s2[-1] == s1[-1]:
            possible.append(self.minimumDeleteSum(s1[:len(s1) - 1], s2[:len(s2) - 1], cache))

        cache[(s1, s2)] = min(possible)
        return cache[(s1, s2)] 

(or first char)

class Solution:
    def minimumDeleteSum(self, s1: str, s2: str, cache = None) -> int:
        if cache is None:
            cache = {}

        if (s1, s2) in cache:
            return cache[(s1, s2)]

        if s1 == s2 == "":
            return 0
        possible = []
        if len(s2) > 0:
            possible.append(self.minimumDeleteSum(s1, s2[1:], cache) + ord(s2[0]))
        if len(s1) > 0:
            possible.append(self.minimumDeleteSum(s1[1:], s2, cache) + ord(s1[0]))
        if len(s2) > 0 and len(s1) > 0 and s2[0] == s1[0]:
            possible.append(self.minimumDeleteSum(s1[1:], s2[1:], cache))

        cache[(s1, s2)] = min(possible)
        return cache[(s1, s2)] 

We can then calculating any possible combination of delete (and keep) character with provided (s1, s2) pair.

Dynamic programming

By using the same logic, but instead of using string and dictionary, I represent the string by its index i -> s[:i] , which mean when i == 0 -> s[:i] = "" . We need cache array covering [0..=n] (inclusive) range

class Solution:
    def minimumDeleteSum(self, s1: str, s2: str, cache = None) -> int:
        cache = [[0]* (len(s2)+1) for i in range(len(s1)+1)]

        for i in range(0, len(s1)+1):
            for j in range(0, len(s2)+1):
                possible = []
                if j > 0:
                    possible.append(cache[i][j-1] + ord(s2[j-1]))
                if i > 0:
                    possible.append(cache[i-1][j] + ord(s1[i-1]))
                if i > 0 and j > 0 and s1[i-1] == s2[j-1]:
                    possible.append(cache[i-1][j-1])
                if possible:
                    cache[i][j] = min(possible)

        return cache[-1][-1] 

This C code technically do the same thing:

int min(int x, int y){
    if (x == -1)
        return y;
    if (x > y)
        return y;
    return x;
}

int minimumDeleteSum(char * s1, char * s2){
    int len1 = strlen(s1);
    int len2 = strlen(s2);

    int cache[1001][1001] = {0};
    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            int possible = -1;

            if (j > 0) 
                possible = min(possible, cache[i][j - 1] + s2[j - 1]);

            if (i > 0) 
                possible = min(possible, cache[i - 1][j] + s1[i - 1]);

            if (i > 0 && j > 0 && s1[i - 1] == s2[j - 1]) 
                possible = min(possible, cache[i - 1][j - 1]);

            if (possible > 0) 
                cache[i][j] = possible;
        }
    }

    return cache[len1][len2];
}


Last update : October 13, 2023
Created : August 16, 2023