583. Delete Operation for Two Strings 72. Edit Distance

583. Delete Operation for Two Strings

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.

1. dp[i][j]: the minimum number of times to deleted a letter。

the string word1 that ends in i-1

the string word2 that ends in j-1

2. recursive formula

When word1[i - 1] and word2[j - 1] are the same

When word1[i - 1] and word2[j - 1] are not the same

When word1[i - 1] is the same as word2[j - 1], dp[i][j] = dp[i - 1][j - 1];

When word1[i - 1] is not the same as word2[j - 1], there are three cases:

Case 1: delete word1[i - 1], the minimum number of operations is dp[i - 1][j] + 1

Case 2: delete word2[j - 1], the minimum number of operations is dp[i][j - 1] + 1

**Case 3:**delete word1[i - 1] and word2[j - 1] at the same time, the minimum number of operations for dp[i - 1][j - 1] + 2

then finally of course the minimum value, so when word1[i - 1] and word2[j - 1] are not the same, recursive formula: dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});

Since dp[i][j - 1] + 1 = dp[i - 1][j - 1] + 2, the recursive formula can be simplified to

dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;

DP:

Time complexity: O(m x n)

Space complexity: O(m x n)

python 复制代码
class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        m = len(word1)
        n = len(word2)

        dp = [[0] * (n + 1) for _ in range(m + 1)]

        for i in range(m + 1): # m+1 不是 m
            dp[i][0] += i
        for j in range(n + 1):
            dp[0][j] += j
        
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if word1[i - 1] == word2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1]
                else:
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1#dp[i][j] = min(dp[i-1][j-1] + 2, dp[i-1][j] + 1, dp[i][j-1] + 1)
        
        return dp[-1][-1]

72. Edit Distance

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

You have the following three operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character

recursive formula:

if (word1[i - 1] == word2[j - 1])

不操作 dp[i][j] = dp[i - 1][j - 1]

if (word1[i - 1] != word2[j - 1])

dp[i][j] = dp[i - 1][j] + 1,dp[i][j] = dp[i][j - 1] + 1 Adding an element to word2 is equivalent to removing an element from word1

删 dp[i][j] = dp[i - 1][j] + 1,dp[i][j] = dp[i][j - 1] + 1

dp[i][j] = dp[i - 1][j - 1] + 1

DP:

Time complexity: O(m x n)

Space complexity: O(m x n)

python 复制代码
class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        m = len(word1)
        n = len(word2)

        dp = [[0] * (n + 1) for _ in range(m + 1)]

        for i in range(m + 1):
            dp[i][0] = i
        for j in range(n + 1):
            dp[0][j] = j
        
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if word1[i - 1] == word2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1]
                else:
                    dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1)
        
        return dp[-1][-1]
相关推荐
liweiweili1261 分钟前
main栈帧和func栈帧的关系
数据结构·算法
Greedy Alg27 分钟前
LeetCode 560. 和为 K 的子数组
算法·leetcode·职场和发展
2501_9248772134 分钟前
强逆光干扰漏检率↓78%!陌讯多模态融合算法在光伏巡检的实战优化
大数据·人工智能·算法·计算机视觉·目标跟踪
2501_924877351 小时前
智慧零售漏扫率↓79%!陌讯多模态融合算法在智能收银与货架管理的实战解析
大数据·人工智能·算法·目标检测·边缘计算·零售
爱编程的鱼2 小时前
C# 数组&C# 多维数组
数据结构·算法·c#
前端开发工程师请求出战2 小时前
MCP — 让AI变得更“百搭”
算法
人机与认知实验室3 小时前
人机环境空战矩阵
人工智能·线性代数·算法·机器学习·矩阵
百度Geek说3 小时前
5个技巧让文心快码成为你的后端开发搭子
后端·算法
上海迪士尼353 小时前
除自身以外数组的乘积是什么意思
数据结构·算法
数据智能老司机3 小时前
Python 实战遗传算法——遗传算法导论
python·算法·机器学习