> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum ASCII Delete Sum for Two Strings

> Tested Python solution for LeetCode 712 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 712, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), Longest Common Subsequence. [View on LeetCode](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 712   # by problem number
lcpy gen -s minimum_ascii_delete_sum_for_two_strings   # by problem name
```

## Problem

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

### Examples

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_ascii_delete_sum_for_two_strings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_ascii_delete_sum_for_two_strings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(min(m, n))
    def minimum_delete_sum(self, s1: str, s2: str) -> int:
        if len(s1) < len(s2):
            s1, s2 = s2, s1
        prev = [0] * (len(s2) + 1)
        for j in range(1, len(s2) + 1):
            prev[j] = prev[j - 1] + ord(s2[j - 1])
        for i in range(1, len(s1) + 1):
            curr = [prev[0] + ord(s1[i - 1])]
            for j in range(1, len(s2) + 1):
                if s1[i - 1] == s2[j - 1]:
                    curr.append(prev[j - 1])
                else:
                    curr.append(min(prev[j] + ord(s1[i - 1]), curr[j - 1] + ord(s2[j - 1])))
            prev = curr
        return prev[-1]
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(min(m, n)) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.