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]
相关推荐
CoovallyAIHub13 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP14 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo14 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo14 小时前
300:最长递增子序列
算法
CoovallyAIHub19 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub20 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI2 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v2 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农2 天前
【React用到的一些算法】游标和栈
算法·react.js