72. Edit Distance

72. Edit Distance

python 复制代码
class Solution:

    def minDistance(self, word1: str, word2: str) -> int:
        m=len(word1)
        n=len(word2)

        dp=[[0 for j in range(n+1)] for i in range(m+1)]

        for i in range(1,m+1):
            dp[i][0]=i
        for j in range(1,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],dp[i-1][j],dp[i][j-1])+1
        return dp[m][n]

0,i),\[0,j\] 匹配需要多少op

相关推荐
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
_深海凉_10 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
踩坑记录11 小时前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode
superior tigre14 小时前
78 子集
算法·leetcode·深度优先·回溯
superior tigre15 小时前
739 每日温度
算法·leetcode·职场和发展
6Hzlia16 小时前
【Hot 100 刷题计划】 LeetCode 15. 三数之和 | C++ 排序+双指针
c++·算法·leetcode
北顾笙98017 小时前
day37-数据结构力扣
数据结构·算法·leetcode
6Hzlia20 小时前
【Hot 100 刷题计划】 LeetCode 189. 轮转数组 | C++ 三次反转经典魔法 (O(1) 空间)
c++·算法·leetcode
m0_6294947320 小时前
LeetCode 热题 100-----13.最大子数组和
数据结构·算法·leetcode
田梓燊20 小时前
力扣:94.二叉树的中序遍历
数据结构·算法·leetcode