LeetCode-72 Edit Distance

Description

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example

Example 1:
Input: word1 = “horse”, word2 = “ros”
Output: 3
Explanation:
horse -> rorse (replace ‘h’ with ‘r’)
rorse -> rose (remove ‘r’)
rose -> ros (remove ‘e’)

Example 2:
Input: word1 = “intention”, word2 = “execution”
Output: 5
Explanation:
intention -> inention (remove ‘t’)
inention -> enention (replace ‘i’ with ‘e’)
enention -> exention (replace ‘n’ with ‘x’)
exention -> exection (replace ‘n’ with ‘c’)
exection -> execution (insert ‘u’)

Submissions

这道题中编辑距离指的是在两个单词<w1,w2>之间,由其中一个单词w1转换为另一个单词w2所需要的最少单字符编辑操作次数。因此解题思路是利用二维数组来记录子串的最小操作数。

其中,单字符编辑操作有且仅有三种:插入,删除和替换。我们将两个字符串a,b的编辑距离表示为 leva,b(|a|,|b|),其中|a|和 |b|分别对应a,b 的长度。那么,在这里两个字符串 a,b的编辑距离,即leva,b(|a|,|b|)可用如下的数学语言描述:
在这里插入图片描述
(1)定义 leva,b(i, j) 指的是 a 中前 i 个字符和 b 中前 j 个字符之间的距离。为了方便理解,这里的i,j可以看作是a,b的长度。这里的字符串的第一个字符 index 从 1 开始(实际因为在表上运算的时候字符串前需要补 0),因此最后的编辑距离便是 i = |a|, j = |b| 时的距离: leva,b(|a|, |b|)
(2)当 min(i, j) = 0 的时候,对应着字符串a 中前 i 个字符和字符串b 中前 j 个字符,此时的 i,j 有一个值为 0 ,表示字符串 a 和 b 中有一个为空串,那么从 a 转换到 b 只需要进行max(i, j)次单字符编辑操作即可,所以它们之间的编辑距离为 max(i, j),即 i, j 中的最大者。
(3)当 min(i, j) ≠ 0 的时候,leva,b(|a|, |b|) 为如下三种情况的最小值:
① leva,b(i-1, j) + 1 表示 删除 ai
② leva,b(i, j-1) + 1 表示 插入 bj
③ leva,b(i-1, j-1)+1( ai ≠ bj)表示 替换 bj
(4)1(ai ≠ bj)为一个指示函数,表示当 ai = bj 的时候取 0 ;当 ai≠bj 的时候,其值为 1。

根据上面的“编辑距离算法”我们使用动态规划来解决这个题目。首先定义一个二维数组distance,distance[i][j]表示字符串word1中前i个字符和word2中前j个字符之间的距离,此处的i、j可以看作word1和word2的这两个字符串的长度。再根据上面的算法,我们可以得出distance元素之间的关系式,即:
(1)if min(i,j) ==0, 那么 distance[i][j] = max(i,j);
(2)if min(i,j) != 0,那么
①if word1[i] == word2[j],那么distance[i][j] = min(distance[i-1][j]+1,distance[i][j-1]+1,distance[i-1][j-1])
②if word1[i] != word2[j],那么distance[i][j] = min(distance[i-1][j]+1,distance[i][j-1]+1,distance[i-1][j-1]+1)
最终,distance[|word1|][|word|2]即为所求的最小值。

实现代码如下:

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        m, n = len(word1), len(word2)
        if word1 == word2 or m == n == 0: return 0
        distance = [[0] * (n+1)for _ in range(m+1)]
        for i in range(m+1):
            distance[i][0] = i
        for j in range(n+1):
            distance[0][j] = j
        for i in range(1,m+1):
            for j in range(1,n+1):
                delete = distance[i-1][j] + 1
                insert = distance[i][j-1] + 1
                swap = distance[i-1][j-1]
                if word1[i-1] != word2[j-1]:
                    swap += 1
                distance[i][j] = min(delete,insert,swap)
        return distance[i][j]

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值