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]
相关推荐
Tairitsu_H3 分钟前
[LC优选算法#2] 滑动窗口 | 长度最小的子数组 | 无重复字符的最长子串 | 最大连续1的个数
算法
小欣加油5 分钟前
leetcode3689最大子数组总值I
c++·算法·leetcode·职场和发展·贪心算法
下午写HelloWorld12 分钟前
【概念与应用】轻量级加密算法LEA、动态脱敏算法DDA、零知识证明ZKP和优化协同交互协议OCIP
算法·区块链·密码学·安全架构·零知识证明
飞舞哲32 分钟前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab
Coder-magician39 分钟前
《代码随想录》刷题打卡day12:二叉树part02
数据结构·c++·算法
海梨花1 小时前
字节面试高频算法题
java·算法·面试·职场和发展
aqiu1111111 小时前
python02
算法
瓦特what?1 小时前
位运算核心技巧与应用
java·jvm·算法
无限码力1 小时前
阿里算法岗 0530笔试真题 - 荆棘林的最优砍断计划
算法·阿里笔试真题·阿里机试真题·阿里算法岗笔试真题·阿里巴巴笔试真题
随意起个昵称1 小时前
线性dp-LIS题目5(导弹拦截,二分优化)
c++·算法·动态规划