其他分享
首页 > 其他分享> > LeetCode 0072 Edit Distance

LeetCode 0072 Edit Distance

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析

1> 定义: dp[i][j] = s1[i] 与 s2[j] 之间的距离 dim(dp) = (m, n)
2> 边界,是转换一个string到空string,使用删除,所以dp值为原始string的长度
dp[i][0] = i, dp[0][j] = j
3> 状态转移方程:
if s1[i] == s2[j]
dp[i][j] = dp[i-1][j-1]
if s1[i] != s2[j]
a) 替换s2[j] -> s1[i]: dp[i][j] = dp[i - 1][j-1] + 1
b) 删除s1[i]: dp[i][j] = dp[i-1][j] + 1
c) 插入 s1[0...i] + s2[j-1] = s2[0...j]: dp[i][j] = dp[i][j-1] + 1
综上,dp[i][j] = min{dp[i-1][j-1], dp[i-1][j], dp[i][j-1]} + 1

dp[i][j] = dp[i-1][j-1], if s1[i] == s2[j]
dp[i][j] = min{dp[i-1][j-1], dp[i-1][j], dp[i][j-1]} + 1, if s1[i] != s2[j]
i in [1, m) , j in [1, n)

2、代码实现

package Q0099.Q0072EditDistance;

/*
    DP
    1. Define the state dp[i][j]
        to be the minimum number of operations to convert word1[0...i) to word2[0...j)
    2. Initial state
        For the base case, that is, to convert a string to an empty string, the minimum number of operations(deletions)
        is just the length of the string. So we have dp[i][0] = i and dp[0][j] = j.
    3. State transition equation
        For the general case to convert word1[0...i) to word2[0...j), we break this problem down into sub-problems.
        Suppose we have already known how to convert word1[0...i-1) to word2[0...j-1) i.e. dp[i-1][j-1],
        if word1[i-1] == word2[j-1], then no more operation is needed and dp[i][j] = dp[i-1][j-1]

        if word1[i-1] != word2[j-1], we need to consider three cases.
        a) Replace word1[i-1] by word2[j-1] i.e. dp[i][j] = dp[i-1][j-1] + 1
        b) if word1[0...i-1) = word2[0...j) then delete word1[i-1] i.e. dp[i][j] = dp[i-1][j] + 1
        c) if word1[0...i) + word2[j-1) = word2[0...j) then insert word2[j-1] to word1[0...i)
            i.e. dp[i][j] = dp[i][j-1] + 1
    So when word1[i-1] != word2[j-1], dp[i][j] will just be the minimum of the above three case
*/
public class Solution1 {
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) dp[i][0] = i;
        for (int j = 1; j <= n; j++) dp[0][j] = j;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1))
                    dp[i][j] = dp[i - 1][j - 1];
                else
                    dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
            }
        }
        return dp[m][n];
    }
}

3、复杂度分析
时间复杂度: O(m * n)
空间复杂度: O(m * n)

3. Solution 2

1、思路分析
将低空间复杂度,使用pre,cur保存上一行和当前行的状态。
2、代码实现

package Q0099.Q0072EditDistance;

import java.util.Arrays;

/*
    Note that each time when we update dp[i][j], we only need dp[i-1][j-1], dp[i][j-1] and dp[i-1][j].
    We may optimize the space of the code to use only two vectors.
 */
public class Solution2 {
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[] pre = new int[n + 1];
        int[] cur = new int[n + 1];
        for (int i = 1; i <= n; i++) pre[i] = i;
        for (int i = 1; i <= m; i++) {
            cur[0] = i;
            for (int j = 1; j <= n; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1))
                    cur[j] = pre[j - 1];
                else
                    cur[j] = Math.min(pre[j - 1], Math.min(cur[j - 1], pre[j])) + 1;
            }
            // cur -> pre
            System.arraycopy(cur, 0, pre, 0, cur.length);
            Arrays.fill(cur, 0);
        }
        return pre[n];
    }
}

3、复杂度分析
时间复杂度: O(m * n)
空间复杂度: O(n)

4. Solution 3

1、思路分析
进一步降低空间复杂度,只使用一行。
2、代码实现

package Q0099.Q0072EditDistance;

/*
    Or even just one array.
 */
public class Solution3 {
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length(), pre;
        int[] cur = new int[n + 1];
        for (int i = 1; i <= n; i++) cur[i] = i;
        for (int i = 1; i <= m; i++) {
            pre = cur[0];
            cur[0] = i;
            for (int j = 1; j <= n; j++) {
                int temp = cur[j];
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    cur[j] = pre;
                } else {
                    cur[j] = Math.min(pre, Math.min(cur[j - 1], cur[j])) + 1;
                }
                pre = temp;
            }
        }
        return cur[n];
    }
}

3、复杂度分析
时间复杂度: O(m * n)
空间复杂度: O(n)

标签:Distance,...,Edit,复杂度,int,0072,word1,word2,dp
来源: https://www.cnblogs.com/junstat/p/16184296.html