72. Edit Distance
72. Edit Distance
0. 参考文献
序号 | 文献 |
---|---|
1 | LeetCode 72. Edit Distance 最短字符串编辑距离 动态规划 |
1.题目
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')
2. 思路
这道题是求解字符串word1 通过替换,删除,插入最小几步可以变成word2。本题也可以通过动态规划的方式解决。设dp[i] [j] 是到word1的0 - i个字符通过替换,删除,插入等步骤可以变成Word2的0 - j 个字符的步骤数。则dp[i] [j] 的递推式有:
-
如果word1[ i - 1 ] 和word2[ j - 1 ] 相同,(注意这里dp矩阵是从空字符开始,所以i-1是j -1 是真实的字符index),则当前的字符不用替换,那么dp[i] [j] 的值就是dp[i-1] [j-1] 的值。
-
如果word1[ i - 1 ] 和word2[ j - 1 ] 不相同,那么要word1[ 0 - i ] 到word2[ 0 - j ]的方法有3种:删除,替换,插入。则当前的值是这3个方法中的最小值:
2.1 删除对应的是dp[ i - 1 ] [ j ] + 1,既word1[ 0 - i -1 ]已经能匹配 word2[ 0 - j ]了,所以删除当前字符。
2.2 插入对应的是dp[ i ] [ j - 1 ] + 1 ,既word1[ 0 - i ] 已经能匹配wrod2[ 0 - j -1 ]了,当前必须在插入一个和word2[j]一样的字符来转换。
2.3 替换对应的是dp[ i - 1 ] [ j - 1 ] +1 ,既word1[ 0 - i -1 ]能匹配word2[ 0 - j -1 ]了,当前的word1[ i ] 和word2[ j ]不一样,必须替换。
同时必须初始化下第0行和第0列 ,(注意dp的0行和0列分别代表了2个空字符串):
for i in range(1,row) :
dp[i][0] = i
for j in range(1,col):
dp[0][j] = j
3. 实现
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
row = len(word1) + 1
col = len(word2) + 1
dp = [ [ 0 for j in range(col) ] for i in range(row) ]
if len(word1) == 0 :
return len(word2)
if len(word2) == 0 :
return len(word1)
dp[0][0] = 0
for i in range(1,row) :
dp[i][0] = i
for j in range(1,col):
dp[0][j] = j
for i in range(1,row):
for j in range(1,col):
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] + 1, dp[i-1][j] + 1);
dp[i][j] = min(dp[i][j-1] + 1, dp[i][j])
return dp[row-1][col-1]