72. Edit Distance leetcode

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()];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值