题目描述:
给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例 1:
输入: word1 = "horse", word2 = "ros"
输出: 3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例 2:
输入: word1 = "intention", word2 = "execution"
输出: 5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')
Solution:
这道题题目大意是将word1转化成word2需要几步,然后找出最少的步数,既然是求解最短问题,那么可以采用动态规划的算法:
一经选择了动态规划,一般就需要一个二维数组来表示求解答案。我们设一个distance[i][j]表示word1的前i个字母到word2的前j个字母需要几步才能达到相同。
例如(转自斯坦福大学NLP公开课:Minimum Edit Distance):
允许的编辑操作包括:将一个字符替换成另一个字符(substitution,s),插入一个字符(insert,i)或者删除一个字符(delete,d),如下图所示:
形式化定义如下:
最终求得D(n,m)即为字符串word1[0…m]与word2[0…n]之间的最小编辑距离。
CODE:
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m, n = len(word1), len(word2)
if word1 == word2 or m == n == 0: return 0
distance = [[0] * (n+1)for _ in range(m+1)]
for i in range(m+1):
distance[i][0] = i
for j in range(n+1):
distance[0][j] = j
for i in range(1,m+1):
for j in range(1,n+1):
delete = distance[i-1][j] + 1
insert = distance[i][j-1] + 1
swap = distance[i-1][j-1]
if word1[i-1] != word2[j-1]:
swap += 1
distance[i][j] = min(delete,insert,swap)
return distance[i][j]