Problem
# Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
# (each operation is counted as 1 step.)
#
# You have the following 3 operations permitted on a word:
#
# a) Insert a character
# b) Delete a character
# c) Replace a character
Idea 1
From [LeetCode] Edit Distance:
思路:
很多算法教科书上都有的经典二维DP问题。
1. 状态:
DP[i+1][j+1]:word1[0:i] -> word2[0:j]的edit distance。
2. 通项公式:
考虑word1[0:i] -> word2[0:j]的最后一次edit。无非题目中给出的三种方式:
a) 插入一个字符:word1[0:i] -> word2[0:j-1],然后在word1[0:i]后插入word2[j]
DP[i+1][j+1] = DP[i+1][j]+1
b) 删除一个字符:word1[0:i-1] -> word2[0:j],然后删除word1[i]
DP[i+1][j+1] = DP[i][j+1]+1
c) 替换一个字符:word1[0:i-1] -> word2[0:j-1]
word1[i] != word2[j]时,word1[i] -> word2[j]:DP[i+1][j+1] = DP[i][j] + 1
word1[i] == word2[j]时:DP[i+1][j+1] = DP[i][j]
所以min editor distance应该为:
DP[i+1][j+1] = min(DP[i][j] + k, DP[i+1][j]+1, DP[i][j+1]+1)
word1[i]==word2[j] -> k = 0, 否则k = 1
3. 计算方向:
replace (i, j) delete (i, j+1)
insert (i+1, j) (i+1, j+1)
可见要求DP[i+1][j+1],必须要知道二维矩阵中左上,上方和下方的3个值。所以当我们确定第0行和第0列的值后,就可以从上到下、从左到右的计算了。
4. 起始、边界值
DP[0][i] = i: word1为空,要转化到word2[0:i-1],需要添加i个字符。
DP[i][0] = i: word2为空,要从word1转化到空字符串,需要删除i个字符。
Idea 2
From [LeetCode] Edit Distance 编辑距离:
这道题让求从一个字符串转变到另一个字符串需要的变换步骤,共有三种变换方式,插入一个字符,删除一个字符,和替换一个字符。
根据以往的经验,对于字符串相关的题目十有八九都是用动态规划Dynamic Programming来解,这道题也不例外。
跟以往的DP题目类似,难点还是在于找出递推式。
这道题我们需要维护一个二维的数组dp,其中dp[i][j]表示从word1的前i个字符转换到word2的前j个字符所需要的步骤。
比如word1是“bbc”,word2是”abcd“,那么我们可以得到dp数组如下:
Ø a b c d
Ø 0 1 2 3 4
b 1 1 1 2 3
b 2 2 1 2 3
c 3 3 2 1 2
我们通过观察可以发现,当word1[i] == word2[j]时,dp[i][j] = dp[i - 1][j - 1],其他情况时,dp[i][j]是其左,左上,上的三个值中的最小值加1,那么可以得到递推式为:
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], min(dp[i - 1][j], dp[i][j - 1])) + 1
AC
class Solution():
def minDistance(self, word1, word2):
distance = [[i] for i in range(len(word1) + 1)]
distance[0] = [j for j in range(len(word2) + 1)]
for i in range(1, len(word1) + 1):
for j in range(1, len(word2) + 1):
insert = distance[i][j - 1] + 1
delete = distance[i - 1][j] + 1
replace = distance[i - 1][j - 1] if word1[i - 1] == word2[j - 1] else distance[i - 1][j - 1] + 1
distance[i].append(min(insert, delete, replace))
return distance[-1][-1]
# Time: O(n * m)
# Space: O(n + m)
class Solution():
def minDistance(self, word1, word2):
if len(word1) < len(word2):
return self.minDistance(word2, word1)
distance = [i for i in range(len(word2) + 1)]
for i in range(1, len(word1) + 1):
pre_distance_i_j = distance[0]
distance[0] = i
for j in range(1, len(word2) + 1):
insert = distance[j - 1] + 1
delete = distance[j] + 1
replace = pre_distance_i_j
if word1[i - 1] != word2[j - 1]:
replace += 1
pre_distance_i_j = distance[j]
distance[j] = min(insert, delete, replace)
return distance[-1]
if __name__ == "__main__":
assert Solution().minDistance("Rabbit", "Racket") == 3
assert Solution().minDistance("Rabbit", "Rabbt") == 1
assert Solution().minDistance("Rabbit", "Rabbitt") == 1