# 583. Delete Operation for Two Strings

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/delete-operation-for-two-strings/

> 内容描述

```
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
The length of given words won't exceed 500.
Characters in given words can only be lower-case letters.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(m * n)******- 空间复杂度: O(m * n)******

其实就是求出word1和word2的最长公共子序列lcs之后，然后对word1和word2进行删除操作直到两者都变成lcs为止所需要的步骤

beats 88.62%

```python
class Solution:
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        return len(word1+word2) - 2 * self.longestCommonSubsequence(word1, word2)
        
    def longestCommonSubsequence(self, s1, s2):  # O(mn)
        m, n = len(s1), len(s2)
        dp = [[0] * (n + 1) for i in range(m + 1)]
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if s1[i - 1] == s2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1] + 1
                else:
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
        return dp[-1][-1]
```


> 思路 2
******- 时间复杂度: O(m * n)******- 空间复杂度: O(m * n)******

dp[i][j]代表想让word1[:i]和word2[:j]相等所需要的操作步骤

那么：
- 当i或者j等于0的时候，我们直到需要删除掉另外一个字符串的所有字符，因此dp[i][j] = i + j
- 当word1[i] == word2[j]时，dp[i][j] = dp[i-1][j-1],因为match了，我们不需要做任何删除操作
- 当word1[i] != word2[j]时，dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1，因为我们肯定要删除一个，但是要删除需要步骤更少的那一个

beats 71.95%

```python
class Solution:
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        m, n = len(word1), len(word2)
        dp = [[0] * (n + 1) for i in range(m + 1)]
        for i in range(m + 1):
            for j in range(n + 1):
                if i == 0 or j == 0:
                    dp[i][j] = i + j
                elif word1[i-1] == word2[j-1]: # match了则不需要删除
                    dp[i][j] = dp[i-1][j-1]
                else:
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1
        return dp[-1][-1]
```

> 思路 3
******- 时间复杂度: O(m * n)******- 空间复杂度: O(n)******

优化空间, beats 88.62%


```python
class Solution:
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        m, n = len(word1), len(word2)
        dp = [0] * (n + 1)
        for i in range(m + 1):
            tmp = [0] * (n + 1)
            for j in range(n + 1):
                if i == 0 or j == 0:
                    tmp[j] = i + j
                elif word1[i-1] == word2[j-1]: # match了则不需要删除
                    tmp[j] = dp[j-1]
                else:
                    tmp[j] = min(dp[j], tmp[j-1]) + 1
            dp = tmp
        return dp[-1]
```



































