题目描述
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]