# Minimum ASCII Delete Sum for Two Strings

## Description

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]`.

## Solution

Let `dp[i + 1][j + 1]` be the minimum delete sum of `s1[0..i]` and `s2[0..j]`.

* If `s1[i] == s2[j]`, then
  * `dp[i + 1][j + 1] = dp[i][j]`
* Otherwise, delete either `s1[i]` or `s2[j]` or both. So,
  * `dp[i + 1][j + 1] = min(dp[i][j + 1] + s1[i], dp[i + 1][j] + s2[j])`
  * Both `dp[i][j + 1]` and `dp[i][j + 1]` contain the case where both `s1[i]` and `s2[j]` are deleted.

Let $$n_1,n_2$$ be the length of `s1` and `s2`, respectively.

The time complexity is $$O(n_1 n_2)$$. The space complexity is also $$O(n_1 n_2)$$, but it can be optimized to $$O(\min(n_1, n_2))$$.

```cpp
class Solution {
public:
    int minimumDeleteSum(string s1, string s2) {
        int n1 = s1.size();
        int n2 = s2.size();
        vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1));
        dp[0][0] = 0;
        for(int i = 0; i < n1; ++i){
            dp[i + 1][0] = dp[i][0] + s1[i];
        }
        for(int j = 0; j < n2; ++j){
            dp[0][j + 1] = dp[0][j] + s2[j];
        }
        for(int i = 0; i < n1; ++i){
            for(int j = 0; j < n2; ++j){
                if(s1[i] == s2[j]){
                    dp[i + 1][j + 1] = dp[i][j];
                }else{
                    dp[i + 1][j + 1] = min(dp[i][j + 1] + s1[i], dp[i + 1][j] + s2[j]);
                }
            }
        }
        return dp[n1][n2];
    }
};
```
