[Leetcode]Edit Distance

来源:互联网 发布:网络传输的结构形状 编辑:程序博客网 时间:2024/06/01 19:25

Given two words word1 and word2, find the minimum number of steps required to convertword1 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

class Solution {public:    /*algorithm: DP        Let d(i, j) be distance between str1 (len = i) and str2 (len = j)        if min{i, j} = 0, d(i, j) = max{i, j}        else d(i, j) = min{ d(i - 1, j) + 1,  // add last char in str1                    d(i, j - 1) + 1,  // add last char in str2                   d(i - 1, j - 1) + (str1[i] == str2[j]) ? 0 : 1} // replace the last char in str1 with that in str2 when str1[i] != str2[j] }        space O(m*n) time O(m*n)    */    int minDistance(string word1, string word2) {        int m = word1.size(),n = word2.size();        vector<vector<int> >dp(m+1,vector<int>(n+1,0));        for(int i = 0;i <= m;i++){            for(int j = 0;j <= n;j++){                if(i==0||j==0)dp[i][j] = max(i,j);                else{                    dp[i][j] = min(min(dp[i-1][j] + 1,dp[i][j-1]+1),dp[i-1][j-1] + (word1[i-1] != word2[j-1]));                }            }        }        return dp[m][n];    }};

class Solution {public:    /*algorithm: DP        Let d(i, j) be distance between str1 (len = i) and str2 (len = j)        if min{i, j} = 0, d(i, j) = max{i, j}        else d(i, j) = min{ d(i - 1, j) + 1,  // add last char in str1                    d(i, j - 1) + 1,  // add last char in str2                   d(i - 1, j - 1) + (str1[i] == str2[j]) ? 0 : 1} // replace the last char in str1 with that in str2 when str1[i] != str2[j] }        becaue it use previous , optimize memory to o(n)        space O(m*n) time O(m*n)    */    int minDistance(string word1, string word2) {        int m = word1.size(),n = word2.size();        vector<int>cur(n+1),prev;        for(int i = 0;i <= m;i++){            for(int j = 0;j <= n;j++){                if(i==0 || j==0)cur[j] = max(i,j);                else cur[j] = min(min(prev[j] + 1,cur[j-1]+1),prev[j-1] + (word1[i-1] != word2[j-1]));            }            prev = cur;        }        return cur[n];    }};

class Solution {public:    /*algorithm: DP        Let d(i, j) be distance between str1 (len = i) and str2 (len = j)        if min{i, j} = 0, d(i, j) = max{i, j}        else d(i, j) = min{ d(i - 1, j) + 1,  // add last char in str1                    d(i, j - 1) + 1,  // add last char in str2                   d(i - 1, j - 1) + (str1[i] == str2[j]) ? 0 : 1} // replace the last char in str1 with that in str2 when str1[i] != str2[j] }        becaue it use previous , optimize memory to o(n)        space O(n) time O(m*n)    */    int minDistance(string word1, string word2) {        int m = word1.size(),n = word2.size();        vector<int>dp(n+1);        int upLeft,minValue; //dp[i-1][j-1]        //first row        for(int j = 0;j <= n;j++){            dp[j] = j;        }        for(int i = 1;i <= m;i++){            upLeft = dp[0];            dp[0] = i;            for(int j = 1;j <= n;j++){                minValue = min(min(dp[j] + 1,dp[j-1]+1),upLeft + (word1[i-1] != word2[j-1]));                upLeft = dp[j];                dp[j] = minValue;            }        }        return dp[n];    }};


0 0