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. dpij: 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 word1i - 1 and word2j - 1 are the same

When word1i - 1 and word2j - 1 are not the same

When word1i - 1 is the same as word2j - 1, dpij = dpi - 1j - 1;

When word1i - 1 is not the same as word2j - 1, there are three cases:

Case 1: delete word1i - 1, the minimum number of operations is dpi - 1j + 1

Case 2: delete word2j - 1, the minimum number of operations is dpij - 1 + 1

**Case 3:**delete word1i - 1 and word2j - 1 at the same time, the minimum number of operations for dpi - 1j - 1 + 2

then finally of course the minimum value, so when word1i - 1 and word2j - 1 are not the same, recursive formula: dpij = min({dpi - 1j - 1 + 2, dpi - 1j + 1, dpij - 1 + 1});

Since dpij - 1 + 1 = dpi - 1j - 1 + 2, the recursive formula can be simplified to

dpij = min(dpi - 1j, dpij - 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 (word1i - 1 == word2j - 1)

不操作 dpij = dpi - 1j - 1

if (word1i - 1 != word2j - 1)

dpij = dpi - 1j + 1,dpij = dpij - 1 + 1 Adding an element to word2 is equivalent to removing an element from word1

删 dpij = dpi - 1j + 1,dpij = dpij - 1 + 1

dpij = dpi - 1j - 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]
相关推荐
yszaygr213840 分钟前
Verilog参数化游程编码RLE模块
算法
望易1 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络5 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron20 小时前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者2 天前
J6B vio scenario sample
算法