[Leetcode]@python 72. Edit Distance

题目链接

https://leetcode.com/problems/edit-distance/

题目原文

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

题目大意

求两个单词的编辑距离

编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。

例如将kitten一字转成sitting:

sitten (k→s)
sittin (e→i)
sitting (→g)

解题思路

定义这样一个函数——edit(i, j),它表示第一个字符串的长度为i的子串到第二个字符串的长度为j的子串的编辑距离。

显然可以有如下动态规划公式:
if i == 0 且 j == 0,edit(i, j) = 0
if i == 0 且 j > 0,edit(i, j) = j
if i > 0 且j == 0,edit(i, j) = i
if i ≥ 1 且 j ≥ 1 ,edit(i, j) == min{ edit(i-1, j) + 1, edit(i, j-1) + 1, edit(i-1, j-1) + f(i, j) },当第一个字符串的第i个字符不等于第二个字符串的第j个字符时,f(i, j) = 1;否则,f(i, j) = 0。

代码

class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        len1 = len(word1)
        len2 = len(word2)
        if len1 == 0:
            return len2
        if len2 == 0:
            return len1
        dict = [[0 for i in range(len2+1)] for j in range(len1+1)]

        for j in range(1,len2+1):
            dict[0][j] = j

        for i in range(1,len1+1):
            dict[i][0] = i

        if word1[0] != word2[0]:
            dict[1][1] = 1

        if len1 >= 1 and len2 >= 1:
            for i in range(1, len1+1):
                for j in range(1, len2+1):
                    dict[i][j] = min(dict[i - 1][j] + 1,
                                     dict[i][j - 1] + 1, 
                                dict[i - 1][j - 1] + int(word1[i-1] != word2[j-1]))

        return dict[len1][len2]    
posted @ 2016-01-14 17:04  slurm  阅读(226)  评论(0编辑  收藏  举报