leetcode 72. Edit Distance(动态规划)

文章目录


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

You have the following 3 operations permitted on a word:

Insert a character
Delete a character
Replace a character
Example 1:

Input: word1 = “horse”, word2 = “ros”
Output: 3
Explanation:
horse -> rorse (replace ‘h’ with ‘r’)
rorse -> rose (remove ‘r’)
rose -> ros (remove ‘e’)
Example 2:

Input: word1 = “intention”, word2 = “execution”
Output: 5
Explanation:
intention -> inention (remove ‘t’)
inention -> enention (replace ‘i’ with ‘e’)
enention -> exention (replace ‘n’ with ‘x’)
exention -> exection (replace ‘n’ with ‘c’)
exection -> execution (insert ‘u’)

  • 动态规划

C++

// O(n^2) space 
class Solution {
public:
    int minDistance(string a, string b) {
        int m=a.size(),n=b.size();
        vector<vector<int>> f(m+1,vector<int>(n+1,0));
        for(int i=1;i<=m;i++)f[i][0]=i; // a[0...i) -> b[0...0) needs i operation
        for(int i=1;i<=n;i++)f[0][i]=i; // a[0,0) -> b[0...i) needs i operation
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++){
                if(a[i-1]==b[j-1])f[i][j]=f[i-1][j-1];
                else f[i][j]=min(f[i-1][j-1],min(f[i][j-1],f[i-1][j]))+1; // replace,delete or insert
            }
        return f[m][n];
    }
};
// two vectors O(2*n) space
class Solution {
public:
    int minDistance(string a, string b) {
        int m=a.size(),n=b.size();
        vector<int> pre(n+1,0),cur(n+1,0);
        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(a[i-1]==b[j-1])cur[j]=pre[j-1];
                else cur[j]=min(pre[j-1],min(pre[j],cur[j-1]))+1;
            }
            fill(pre.begin(),pre.end(),0);
            swap(pre,cur);
        }
        return pre[n];
    }
};
// one vector O(n) space
class Solution {
public:
    int minDistance(string a, string b) {
        int m=a.size(),n=b.size(),p;
        vector<int> f(n+1,0);
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++){
            p=f[0];
            f[0]=i;
            for(int j=1;j<=n;j++){
                int tmp=f[j];
                if(a[i-1]==b[j-1])f[j]=p;
                else f[j]=min(p,min(f[j-1],f[j]))+1;
                p=tmp;
            }
        }
        return f[n];
    }
};

Java

class Solution {
    public int minDistance(String a, String b) {
        int m=a.length();
        int n=b.length();
        int[][] f=new int[m+1][n+1];
        for(int i=1;i<=m;i++)
            f[i][0]=i;
        for(int i=1;i<=n;i++)
            f[0][i]=i;
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++){
                if(a.charAt(i-1)==b.charAt(j-1))f[i][j]=f[i-1][j-1];
                else{
                    f[i][j]=Math.min(f[i-1][j-1],Math.min(f[i-1][j],f[i][j-1]))+1;
                }
            }
        return f[m][n];
    }
}
class Solution {
    public int minDistance(String a, String b) {
        int m=a.length();
        int n=b.length();
        // int[][] f=new int[m+1][n+1];
        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(a.charAt(i-1)==b.charAt(j-1))cur[j]=pre[j-1];
                else{
                    cur[j]=Math.min(pre[j-1],Math.min(pre[j],cur[j-1]))+1;
                }
            }
            for(int k=0;k<=n;k++){
                pre[k]=cur[k];
                cur[k]=0;
            }
        }
        return pre[n];
    }
}
class Solution {
    public int minDistance(String a, String b) {
        int m=a.length();
        int n=b.length();
        int p;
        // int[][] f=new int[m+1][n+1];
        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++){
            p=pre[0];
            pre[0]=i;
            for(int j=1;j<=n;j++){
                int tmp=pre[j];
                if(a.charAt(i-1)==b.charAt(j-1))pre[j]=p;
                else{
                    pre[j]=Math.min(p,Math.min(pre[j],pre[j-1]))+1;
                }
                p=tmp;
            }
        }
        return pre[n];
    }
}

Python

class Solution:
    def minDistance(self, a: str, b: str) -> int:
        m,n=len(a),len(b)
        f=[[0]*(n+1) for _ in range(m+1)]
        for i in range(1,m+1):
            f[i][0]=i
        for i in range(1,n+1):
            f[0][i]=i
        for i in range(1,m+1):
            for j in range(1,n+1):
                if a[i-1]==b[j-1]:
                    f[i][j]=f[i-1][j-1]
                else:
                    f[i][j]=min(f[i-1][j-1],min(f[i-1][j],f[i][j-1]))+1
        return f[m][n]

Go

func minDistance(a string, b string) int {
    m,n:=len(a),len(b)
    f:=make([][]int, m+1)
    for i:=range f{
        f[i]=make([]int,n+1)
        f[i][0]=i
    }
    for j:=range f[0]{
        f[0][j]=j
    }
    for i:=1;i<=m;i++ {
        for j:=1;j<=n;j++ {
            if a[i-1]==b[j-1] {
                f[i][j]=f[i-1][j-1]
            } else {
                f[i][j]=min(f[i-1][j-1],min(f[i-1][j],f[i][j-1]))+1
            }
        }
    }
    return f[m][n]
}
func min(a,b int) int {
    if a>b {
        return b
    }
    return a
}
posted @ 2020-08-20 13:15  winechord  阅读(156)  评论(0编辑  收藏  举报