资讯专栏INFORMATION COLUMN

72 Edit Distance 以及两个string比较的填表通解。

ormsf / 3085人阅读

摘要:给出两个解,一个是填表,一个是记忆化搜索。因为填表一定会把的表填满。走出来的则是一条从起点到终点的线,不会填满整个表。时间退化到,变成找路径的时间。

72 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
给出两个解,一个是填dp表,一个是记忆化搜索。效果是memorized search更好。
因为dp填表一定会把O(m*n)的dp表填满。
memorized search走出来的则是一条从起点到终点的线,不会填满整个表。时间退化到O(m+n),变成找路径的时间。
public class Solution {
    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int[][] dp = new int[m+1][n+1];
        
        for(int i=0; i<=m; i++) {
            for(int j=0; j<=n;j++) {
                if(i == 0) {        
                    dp[i][j] = j;
                } else if( j == 0){    
                    dp[i][j] = i;
                } else if(word1.charAt(i-1) == word2.charAt(j-1) ) {  
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = 1 + Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
                }
            }
        }
        return dp[m][n];
    }
}
public class Solution {
    int[][] dp;
    
    public int minDistance(String word1, String word2) {
        dp = new int[word1.length()][word2.length()];
        
        return minDistanceHelper(word1, word2, 0, 0);
    }
    
    private int minDistanceHelper(String word1, String word2, int index1, int index2) {
        if (index1 == word1.length()) return word2.length() - index2;
        if (index2 == word2.length()) return word1.length() - index1;
        
        if (dp[index1][index2] > 0) return dp[index1][index2];
        
        int result;
        if (word1.charAt(index1) == word2.charAt(index2)) {
            result = minDistanceHelper(word1, word2, index1+1, index2+1);
        } else {
            // replace char "abac" , "abdc" replace "a" with "d" 
            result = 1 + minDistanceHelper(word1, word2, index1+1, index2+1);
            
            // insert char into word1    "abc" , "abdc" insert "d"
            result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1, index2+1));
            
            // delete char from word1    "abdc" , "abc"  delete "d"
            result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1+1, index2));
        }
        
        dp[index1][index2] = result;
        return result;
    }
}

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

转载请注明本文地址:https://www.ucloud.cn/yun/66929.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
  • leetcode72. Edit Distance

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

    venmos 评论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
  • ❤️思维导图整理大厂面试高频数组9: 删除重复元素通解问题, 力扣26/80❤️

    此专栏文章是对力扣上算法题目各种方法的总结和归纳, 整理出最重要的思路和知识重点并以思维导图形式呈现, 当然也会加上我对导图的详解. 目的是为了更方便快捷的记忆和回忆算法重点(不用每次都重复看题解), 毕竟算法不是做了一遍就能完全记住的. 所以本文适合已经知道解题思路和方法, 想进一步加强理解和记忆的朋友, 并不适合第一次接触此题的朋友(可以根据题号先去力扣看看官方题解, 然后再看本文内容). 关...

    MasonEast 评论0 收藏0

发表评论

0条评论

ormsf

|高级讲师

TA的文章

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