LeetCode:72. Edit Distance

LeetCode:72. Edit Distance

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:

Insert a character
Delete a character
Replace a character

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')

可以通过插入、删除、替换三种操作将一个字符串变为另一个字符串,给定两个字符串,找出这两个字符串的最小编辑距离(操作次数)。

思路:动态规划

设i是字符串1中的某个位置,j是字符串2中得某个位置,在(i,j)处之前的两个字串的最小编辑距离为cost(i,j)。

首先,如果i位置的字符c1和j位置的字符c2相等,表明(i,j)处不需要操作,所以cost(i,j) = cost(i-1,j-1)。

其次,如果c1和c2不相等,则会有三种情况将word1变换到word2:

  • cost(i,j) = cost(i-1,j)+1 对应了删除wrod1中i处这个字符的操作。
  • cost(i,j) = cost(i,j-1)+1 对应了在wrod1中i处这插入个字符的操作。
  • cost(i,j) = cost(i-1,j-1)+1 对应了在wrod1中i处替换一个字符的操作。

然后取三个操作对应的最小值即可也就是cost(i,j) = min(cost(i-1,j), cost(i,j-1), cost(i-1, j-1)) + 1。

用上面的 horseros 举例:

  1. (i,j) = (0,0),通过替换操作将h变为r,cost(0,0) = 1;
  2. (i,j) = (0,1),在(0,0)基础上通过插入o将h变为ro,cost(0,1) = 2;
  3. (i,j) = (0,2),在(0,1)基础上通过插入s将h变为ros,cost(0,2) = 3;
  4. (i,j) = (1,0),通过删除和替换操作将ho变为r,cost(1,0) = 2;
  5. (i,j) = (1,1),通过替换操作将ho变为ro,cost(1,1) = 1;
  6. 依次类推。

最终得到如下表格:

#01 , r2 , o3 , s
00123
1 , h1123
2, o2212
3 , r3222
4 , s4332
5 , e5443

Python 代码实现

class Solution:
    # 参考 : https://leetcode.com/problems/edit-distance/discuss/25849/Java-DP-solution-O(nm)
    
    def minDistance(self, word1: str, word2: str) -> int:
        m = len(word1)
        n = len(word2)
        cost = [[0 for i in range(n+1)] for j in range(m+1)]

        for i in range(m+1):
            cost[i][0] = i
        for i in range(n+1):
            cost[0][i] = i
 
        for i in range(m):
            for j in range(n):
                if(word1[i] == word2[j]):
                    cost[i + 1][j + 1] = cost[i][j]
                else:
                    a = cost[i][j]
                    b = cost[i][j + 1]
                    c = cost[i + 1][j]
                    cost[i + 1][j + 1] = min(a,min(b,c))+1
        return cost[m][n];

参考:

Java-DP-solution-O(nm)


THE END.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值