Leetcode May Challenge - 05/31: Edit Distance(Python)

题目描述

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

解释

给两个单词,求他们之间的Edit Distance。
Edit Distance的概念(摘自Wikipedia):
In computational linguistics and computer science, edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other.
即两个string把一个变成另一个所需要的步数。每次变换可以是增加一个字母、删除一个字母或者改变一个字母。

思路 动态规划

这道题的思路是,我们用一个二维的dp数组,其中dp[i][j]存的是把string1[:i]变为string2[:j]所需要的步数。
首先确定dp数组的维度。假设两个string的长度分别为m和n,则dp应该是一个(m + 1)*(n + 1)的数组。这是因为我们同样需要考虑到两个string有可能为空字符串的情况。
现在我们来推一下状态转移方程。对于edit distance的计算来说,我们考虑dp[i][j]有可能和哪个之前的状态有关。Edit distance无非三种情况,增加,删除,和替换。在这个数组中,我们同样可以用dp[i-1, j] + 1表示增加操作;dp[i, j-1] + 1 表示我们的删除操作,d[i - 1, j - 1] + notEqual表示我们的替换操作。notEqual是我们每次需要判断的一个变量。如果string1[i] != string2[j],则把notEqual设为1。

代码

class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        m = len(word1)
        n = len(word2)
        if m == 0:
            return n
        if n == 0:
            return m
        
        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):
                notEqual = 0
                if word1[i - 1] != word2[j - 1]:
                    notEqual = 1
                dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + notEqual)
        return dp[-1][-1]
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值