LeetCode Edit Distance DP

原创 2015年07月09日 22:08:24

思路:

方法一:DP。时间复杂度O(N*M),空间复杂度 O(N*M)。

设 f[i][j] 表示 word1[0…i] 与 word2[0…j] 的最小编辑距离,则有:
(1)如果 word1[i] == word2[j],f[i][j] = f[i-1][j-1] ;
(2)如果 word1[i] != word2[j],
如果在word1[i] 后面添加一个字符等于word2[j],则f[i][j] = f[i][j-1] + 1 ;
如果将word1[i]删除,则f[i][j] = f[i-1][j] +1 ;
如果将word1[i]替换为word2[j],则f[i][j] = f[i-1][j-1] + 1 ;

c++:

class Solution {
public:
    int minDistance(string word1, string word2) {
        int n1 = word1.size();
        int n2 = word2.size();
        vector<vector<int>> f = vector<vector<int>>(n1 + 1, vector<int>(n2 + 1, 0));
        for(int i = 0; i <= n1; ++i) {
            f[i][0] = i;
        }
        for(int i = 0; i <= n2; ++i) {
            f[0][i] = i;
        }
        for(int i = 1; i <= n1; ++i) {
            for(int j = 1; j <= n2; ++j) {
                if(word1[i - 1] == word2[j - 1]) {
                    f[i][j] = f[i-1][j-1];
                }else {
                    int tmp = min(f[i][j-1] + 1, f[i-1][j] + 1);
                    f[i][j] = min(tmp, f[i-1][j-1] + 1);
                }
            }
        }
        return f[n1][n2];
    }
};

java:

public class Solution {
    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();

        int[][] cost = new int[m + 1][n + 1];
        for(int i = 0; i <= m; ++i) {
            cost[i][0] = i;
        }
        for(int i = 0; i <= n; ++i) {
            cost[0][i] = i;
        }

        for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) {
                if(word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    cost[i][j] = cost[i - 1][j - 1];
                }else {
                    int tmp = cost[i - 1][j] + 1 < cost[i][j - 1] + 1 ? cost[i - 1][j] + 1 : cost[i][j - 1] + 1;
                    cost[i][j] = tmp < cost[i - 1][j - 1] + 1 ? tmp : cost[i - 1][j - 1] + 1;
                }
            }
        }

        return cost[m][n];
    }
}

方法二:DP+滚动数组。时间复杂度O(N*M),空间复杂度O(N)。

row1: A B
row2: A’ B’

B’ = min(A, A’, B);

要有个变量保存A。

class Solution {
public:
    int minDistance(string word1, string word2) {
        int n = word1.size();
        int m = word2.size();
        if(n < m) {
            minDistance(word2, word1);
        }

        vector<int> f = vector<int>(m + 1, 0);
        for(int i = 0; i <= m; ++i) {
            f[i] = i;
        }

        for(int i = 1; i <= n; ++i) {
            int upper_left = f[0];
            f[0] = i;
            for(int j = 1; j <= m; ++j) {
                int upper = f[j];
                if(word1[i-1] == word2[j-1]) {
                    f[j] = upper_left;
                }else {
                    f[j] = 1 + min(upper_left, min(f[j], f[j-1]));
                }
                upper_left = upper;
            }
        }
        return f[m];
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离DP&DFS

LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离 动态规划

LeetCode 72 Edit Distance DP解法思路记录

leetcode 72 Edit Distance 给定两个字符串,寻找最小的操作数,使得word1变为word2. 1.可以插入字符 2.可以删除字符 3.可以替换字符 对于上面的...

Leetcode 72 - Edit Distance(dp)

题意就一编辑距离的题,支持操作:insert, delete, remove,求从x到y的最小edit distance思路状态表示:d[i][j]d[i][j],从x[0, i]到y[0, j]的最...
  • Lzedo
  • Lzedo
  • 2017-01-23 15:56
  • 130

[LeetCode 72] Edit Distance(编辑距离/动态规划/DP)

72. Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to conve...
  • zlasd
  • zlasd
  • 2016-12-01 23:24
  • 119

POJ 3356 AGTC 解题报告(Edit Distance,DP)

AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9579   Accepted:...
  • kbdwo
  • kbdwo
  • 2014-04-27 19:49
  • 601

leetcode:Edit Distance 动态规划--用于求两个词之间的相似度

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2...

LeetCode072 Edit Distance

详细见:leetcode.com/problems/edit-distance Java Solution: github package leetcode; /** * Giv...

LeetCode 72 Edit Distance (动态规划)

LeetCode 72 Edit Distance (动态规划)

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2...

(Java)LeetCode-72. Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)