资讯专栏INFORMATION COLUMN

leetcode72. Edit Distance

venmos / 1636人阅读

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

题目要求
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

输入两个字符串word1和word2,允许对word1进行插入,删除和替换的操作,计算出将word1转化为word2所需要的最少的操作数。

思路

这是一道典型的dynamic programming的题目,这里我们利用二维数组steps来存储相应的情况。其中steps[i][j]存储的是word1[0..i-1]转换为word2[0..j-1]的最小步数。那么如何计算stepsi呢。
首先从边缘情况开始考虑。

如果i=0,也就是word1=“”,那么将word1转化为word2的最少操作数为j
同理,如果j=0,也就是word2=“”,那么将word2转化为word1的最少操作数为i

这时我们再考虑i,j都不为0的情况。
可能出现以下几种情况:
1.word1[i-1]==word2[j-1]
如果下标相等,那么steps[i][j]=steps[i-1][j-1]。从语义的角度上来说,假设已知将word1[0...i-2]转化为word2[0...j-2]的最小操作数,那么如果彼此下一个值相等,则无需进行操作。
2.word1[i-1]!=word2[j-1]

插入:在word1[i-1]的位置上插入word2[j-1],也就是说word1[0...i-1]=word2[0...j-1],steps[i][j]=steps[i][j-1]+1
替换:将word1[i-1]的值替换为word2[j-1]。steps[i][j]=steps[i-1][j-1]+1
删除:将word1[i-1]的值删除使word1[0...i-2]等价于word2[j-1], steps[i][j] = steps[i-1][j]+1
在这里解释一下插入和删除的算法的原理。
如果插入即可使word1[0...i-1]转化为word2[0...j-1],那么意味着word[0...i-1]可以转换为word2[0...j-2]。只要在此基础上再进行一次插入操作即可以完成转换。所以steps[i][j]=steps[i][j-1]+1
删除操作同理,word[0...i-2]可以转化为word[0...j-1],只需要再此基础上再进行一次删除操作即可,所以steps[i][j] = steps[i-1][j]+1
代码
    public int minDistance(String word1, String word2) {
        if(word1.isEmpty()) return word2.length();
        if(word2.isEmpty()) return word1.length();
        int[][] steps = new int[word1.length()+1][word2.length()+1];
        for(int i = 0 ; i<=word1.length() ; i++){
            for(int j = 0 ; j<=word2.length() ; j++){
                if(i==0){
                    steps[i][j] = j;
                }else if(j==0){
                    steps[i][j] = i;
                }else if(word1.charAt(i-1) == word2.charAt(j-1)){
                    steps[i][j] = steps[i-1][j-1];
                 }else{
                     steps[i][j] = Math.min(Math.min(steps[i][j-1]+1, steps[i-1][j-1]+1), steps[i-1][j]+1);
                 }
                    
            }
        }
        return steps[word1.length()][word2.length()];
    }

优化后的代码如下:

    public int minDistance2(String word1, String word2){
        if(word1.isEmpty()) return word2.length();
        if(word2.isEmpty()) return word1.length();
        int[] steps = new int[word2.length()+1];
        for(int j = 0 ; j<=word2.length() ; j++){
            steps[j] = j;
        }
        for(int i = 1 ; i<=word1.length() ; i++){
            int pre = steps[0];
            steps[0] = i;
            for(int j = 1 ; j<=word2.length() ; j++){
                int temp = steps[j];
                if(word1.charAt(i-1)==word2.charAt(j-1)){
                    steps[j] = pre;
                }else{
                    steps[j] = Math.min(pre+1, Math.min(steps[j]+1, steps[j-1]+1));
                }
                pre = temp;
            }

        }
        return steps[word2.length()];
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

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

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

相关文章

  • LeetCode[72] Edit Distance

    摘要:复杂度思路考虑用二维来表示变换的情况。如果两个字符串中的字符相等,那么如果两个字符串中的字符不相等,那么考虑不同的情况表示的是,从字符串到的位置转换到字符串到的位置,所需要的最少步数。 LeetCode[72] Edit Distance Given two words word1 and word2, find the minimum number of steps require...

    call_me_R 评论0 收藏0
  • 72 Edit Distance 以及两个string比较的填表通解。

    摘要:给出两个解,一个是填表,一个是记忆化搜索。因为填表一定会把的表填满。走出来的则是一条从起点到终点的线,不会填满整个表。时间退化到,变成找路径的时间。 72 Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. ...

    ormsf 评论0 收藏0
  • [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
  • [Leetcode] Edit Distance 最小编辑距离

    摘要:动态规划复杂度时间空间思路这是算法导论中经典的一道动态规划的题。 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 h...

    zhangke3016 评论0 收藏0

发表评论

0条评论

venmos

|高级讲师

TA的文章

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