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

# Delete Operation for Two Strings

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

LeetCode 583, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), Longest Common Subsequence. [View on LeetCode](https://leetcode.com/problems/delete-operation-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 583   # by problem number
lcpy gen -s delete_operation_for_two_strings   # by problem name
```

## Problem

Given two strings `word1` and `word2`, return *the minimum number of steps* required to make `word1` and `word2` the same.

In one step, you can delete exactly one character in either string.

### Examples

```
Input: word1 = "sea", word2 = "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
```

```
Input: word1 = "leetcode", word2 = "etco"
Output: 4
```

### Constraints

* 1 \<= word1.length, word2.length \<= 500
* word1 and word2 consist of only lowercase English letters.

## Solution

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

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

## Complexity

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

## Tags


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