资讯专栏INFORMATION COLUMN

[Leetcode] Edit Distance 最小编辑距离

zhangke3016 / 2979人阅读

摘要:动态规划复杂度时间空间思路这是算法导论中经典的一道动态规划的题。

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

动态规划 复杂度

时间 O(NM) 空间 O(NM)

思路

这是算法导论中经典的一道动态规划的题。假设dp[i-1][j-1]表示一个长为i-1的字符串str1变为长为j-1的字符串str2的最短距离,如果我们此时想要把str1a这个字符串变成str2b这个字符串,我们有如下几种选择:

替换: 在str1变成str2的步骤后,我们将str1a中的a替换为b,就得到str2b (如果ab相等,就不用操作)

增加: 在str1a变成str2的步骤后,我们再在末尾添加一个b,就得到str2b (str1a先根据已知距离变成str2,再加个b)

删除: 在str1变成str2b的步骤后,对于str1a,我们将末尾的a删去,就得到str2b (str1aa删去得到str1,而str1str2b的编辑距离已知)

根据这三种操作,我们可以得到递推式
若a和b相等:

dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i][j])

若a和b不相等:

dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i][j]+1)

因为将一个非空字符串变成空字符串的最小操作数是字母个数(全删),反之亦然,所以:

dp[0][j]=j, dp[i][0]=i

最后我们只要返回dp[m][n]即可,其中m是word1的长度,n是word2的长度

详解请看斯坦福课件

代码
public class Solution {
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        // 初始化空字符串的情况
        for(int i = 1; i <= m; i++){
            dp[i][0] = i;
        }
        for(int i = 1; i <= n; i++){
            dp[0][i] = i;
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                // 增加操作:str1a变成str2后再加上b,得到str2b
                int insertion = dp[i][j-1] + 1;
                // 删除操作:str1a删除a后,再由str1变为str2b
                int deletion = dp[i-1][j] + 1;
                // 替换操作:先由str1变为str2,然后str1a的a替换为b,得到str2b
                int replace = dp[i-1][j-1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1);
                // 三者取最小
                dp[i][j] = Math.min(replace, Math.min(insertion, deletion));
            }
        }
        return dp[m][n];
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64502.html

相关文章

  • [Leetcode] One Edit Distance 编辑距离为一

    摘要:比较长度法复杂度时间空间思路虽然我们可以用的解法,看是否为,但中会超时。这里我们可以利用只有一个不同的特点在时间内完成。 One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 比较长度法 复杂度 时间 O(N) 空间 O(1) 思路 虽然我们可以用...

    lewinlee 评论0 收藏0
  • Leetcode[161] One Edit Distance

    摘要:复杂度思路考虑如果两个字符串的长度,是肯定当两个字符串中有不同的字符出现的时候,说明之后的字符串一定要相等。的长度比较大的时候,说明的时候,才能保证距离为。 LeetCode[161] One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. Stri...

    周国辉 评论0 收藏0
  • leetcode72. Edit Distance

    摘要:题目要求输入两个字符串和,允许对进行插入,删除和替换的操作,计算出将转化为所需要的最少的操作数。其中存储的是转换为的最小步数。首先从边缘情况开始考虑。只要在此基础上再进行一次插入操作即可以完成转换。 题目要求 Given two words word1 and word2, find the minimum number of steps required to convert wor...

    venmos 评论0 收藏0
  • [LintCode/LeetCode] Edit Distance

    摘要:构造数组,是的,是的,是将位的转换成位的需要的步数。初始化和为到它们各自的距离,然后两次循环和即可。 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 s...

    snowell 评论0 收藏0
  • 文本相似度 余弦值相似度算法 VS L氏编辑距离(动态规划)

    摘要:本文对两种文本相似度算法进行比较。余弦值相似度算法最小编辑距离法氏编辑距离基于词条空间编辑距离,又称距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。但是同时也可以看出余弦相似度得到的结果相对比较高一些。 本文由作者祝娜授权网易云社区发布。 本文对两种文本相似度算法进行比较。余弦值相似度算法 VS 最小编辑距离法1、L氏编辑距离(基于词条空间)编辑距离(Edit Dist...

    fxp 评论0 收藏0

发表评论

0条评论

zhangke3016

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<