Number of Ways to Form a Target String Given a Dictionary

Problem

Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.

Examples

Example 1:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
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.

Solution

Method 1 – Dynamic Programming (Bottom-Up)

Intuition

To make two strings equal with the minimum ASCII delete sum, we use dynamic programming to track the minimum cost for each substring pair. At each step, we can delete a character from either string or keep matching characters.

Approach

  1. Use a DP table dp[i][j] where i and j are indices in s1 and s2.
  2. dp[i][j] is the minimum ASCII sum to make s1[i:] and s2[j:] equal.
  3. If s1[i] == s2[j], move to next indices.
  4. Otherwise, delete s1[i] or s2[j] and add their ASCII value.
  5. Fill the table bottom-up.
  6. The answer is dp[0][0].

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
    int minimumDeleteSum(string s1, string s2) {
        int n = s1.size(), m = s2.size();
        vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
        for (int i = n-1; i >= 0; --i)
            dp[i][m] = dp[i+1][m] + s1[i];
        for (int j = m-1; j >= 0; --j)
            dp[n][j] = dp[n][j+1] + s2[j];
        for (int i = n-1; i >= 0; --i) {
            for (int j = m-1; j >= 0; --j) {
                if (s1[i] == s2[j])
                    dp[i][j] = dp[i+1][j+1];
                else
                    dp[i][j] = min(s1[i]+dp[i+1][j], s2[j]+dp[i][j+1]);
            }
        }
        return dp[0][0];
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func minimumDeleteSum(s1, s2 string) int {
    n, m := len(s1), len(s2)
    dp := make([][]int, n+1)
    for i := range dp {
        dp[i] = make([]int, m+1)
    }
    for i := n-1; i >= 0; i-- {
        dp[i][m] = dp[i+1][m] + int(s1[i])
    }
    for j := m-1; j >= 0; j-- {
        dp[n][j] = dp[n][j+1] + int(s2[j])
    }
    for i := n-1; i >= 0; i-- {
        for j := m-1; j >= 0; j-- {
            if s1[i] == s2[j] {
                dp[i][j] = dp[i+1][j+1]
            } else {
                dp[i][j] = min(int(s1[i])+dp[i+1][j], int(s2[j])+dp[i][j+1])
            }
        }
    }
    return dp[0][0]
}
func min(a, b int) int { if a < b { return a }; return b }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public int minimumDeleteSum(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        int[][] dp = new int[n+1][m+1];
        for (int i = n-1; i >= 0; i--)
            dp[i][m] = dp[i+1][m] + s1.charAt(i);
        for (int j = m-1; j >= 0; j--)
            dp[n][j] = dp[n][j+1] + s2.charAt(j);
        for (int i = n-1; i >= 0; i--) {
            for (int j = m-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(s1.charAt(i)+dp[i+1][j], s2.charAt(j)+dp[i][j+1]);
            }
        }
        return dp[0][0];
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    fun minimumDeleteSum(s1: String, s2: String): Int {
        val n = s1.length; val m = s2.length
        val dp = Array(n+1) { IntArray(m+1) }
        for (i in n-1 downTo 0)
            dp[i][m] = dp[i+1][m] + s1[i].code
        for (j in m-1 downTo 0)
            dp[n][j] = dp[n][j+1] + s2[j].code
        for (i in n-1 downTo 0) {
            for (j in m-1 downTo 0) {
                if (s1[i] == s2[j])
                    dp[i][j] = dp[i+1][j+1]
                else
                    dp[i][j] = minOf(s1[i].code+dp[i+1][j], s2[j].code+dp[i][j+1])
            }
        }
        return dp[0][0]
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def minimumDeleteSum(s1: str, s2: str) -> int:
    n, m = len(s1), len(s2)
    dp = [[0]*(m+1) for _ in range(n+1)]
    for i in range(n-1, -1, -1):
        dp[i][m] = dp[i+1][m] + ord(s1[i])
    for j in range(m-1, -1, -1):
        dp[n][j] = dp[n][j+1] + ord(s2[j])
    for i in range(n-1, -1, -1):
        for j in range(m-1, -1, -1):
            if s1[i] == s2[j]:
                dp[i][j] = dp[i+1][j+1]
            else:
                dp[i][j] = min(ord(s1[i])+dp[i+1][j], ord(s2[j])+dp[i][j+1])
    return dp[0][0]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
impl Solution {
    pub fn minimum_delete_sum(s1: String, s2: String) -> i32 {
        let n = s1.len(); let m = s2.len();
        let s1 = s1.as_bytes(); let s2 = s2.as_bytes();
        let mut dp = vec![vec![0; m+1]; n+1];
        for i in (0..n).rev() {
            dp[i][m] = dp[i+1][m] + s1[i] as i32;
        }
        for j in (0..m).rev() {
            dp[n][j] = dp[n][j+1] + s2[j] as i32;
        }
        for i in (0..n).rev() {
            for j in (0..m).rev() {
                if s1[i] == s2[j] {
                    dp[i][j] = dp[i+1][j+1];
                } else {
                    dp[i][j] = (s1[i] as i32 + dp[i+1][j]).min(s2[j] as i32 + dp[i][j+1]);
                }
            }
        }
        dp[0][0]
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    minimumDeleteSum(s1: string, s2: string): number {
        const n = s1.length, m = s2.length;
        const dp: number[][] = Array.from({length: n+1}, () => Array(m+1).fill(0));
        for (let i = n-1; i >= 0; i--)
            dp[i][m] = dp[i+1][m] + s1.charCodeAt(i);
        for (let j = m-1; j >= 0; j--)
            dp[n][j] = dp[n][j+1] + s2.charCodeAt(j);
        for (let i = n-1; i >= 0; i--) {
            for (let j = m-1; j >= 0; j--) {
                if (s1[i] === s2[j])
                    dp[i][j] = dp[i+1][j+1];
                else
                    dp[i][j] = Math.min(s1.charCodeAt(i)+dp[i+1][j], s2.charCodeAt(j)+dp[i][j+1]);
            }
        }
        return dp[0][0];
    }
}

Complexity

  • ⏰ Time complexity: O(n*m), since we fill a DP table for all substring pairs.
  • 🧺 Space complexity: O(n*m), for the DP table.