资讯专栏INFORMATION COLUMN

LeetCode[72] Edit Distance

call_me_R / 848人阅读

摘要:复杂度思路考虑用二维来表示变换的情况。如果两个字符串中的字符相等,那么如果两个字符串中的字符不相等,那么考虑不同的情况表示的是,从字符串到的位置转换到字符串到的位置,所需要的最少步数。

LeetCode[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

复杂度
O(MN), O(MN)

思路
考虑用二维dp来表示变换的情况。
如果两个字符串中的字符相等,a[i] = b[j],那么dp[i][j] = dp[i - 1][j - 1] + 1;
如果两个字符串中的字符不相等,那么考虑不同的情况

insert: dp[i][j] = dp[i][j - 1] + 1;
replace: dp[i][j] = dp[i - 1][j - 1] + 1;
delete: dp[i][j] = dp[i - 1][j] + 1;
dp[i][j]表示的是,从字符串1到i的位置转换到字符串2到j的位置,所需要的最少步数。

代码

public int minDistance(String word1, String word2) {
    int len1 = word1.length();
    int len2 = word2.length();
    int[][] dp = new int[len1 + 1][len2 + 1];
    // initialize the dp array;
    for(int i = 0; i < len1; i ++) {
        dp[i + 1][0] = i + 1;
    }
    for(int j = 0; j < len2; j ++) {
        dp[0][j + 1] = j + 1;
    }
    //
    char[] arr1 = word1.toCharArray();
    char[] arr2 = word2.toCharArray();
    for(int i = 0; i < len1; i ++) {
        for(int j = 0; j < len2; j ++) {
            if(arr1[i] == arr2[j]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else {
                dp[i][j] = Math.min(dp[i - 1][j - 1], 
                    Math.min(dp[i][j - 1], dp[i - 1][j])) + 1;
            }
        }
    }
    return dp[len1][len2];
}

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

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

相关文章

  • leetcode72. Edit Distance

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

    venmos 评论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条评论

call_me_R

|高级讲师

TA的文章

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