72. Edit Distance

Given two wordsword1andword2, 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

Thoughts:

  1. f[i][j]: minimum number of edit distance between word1[0,...i-1] to word2[0,...j-1].
  2. initial state:
    1. f[i][0] = i ( need i operations(deletions) to convert word1[0, i - 1] to empty string)
    2. f[0][j] = j (need j operations (deletions) to convert word2[0, j - 1] to empty string)
  3. recursive state:

    1. if word1[i-1] is equal to word2[j-1]: f[i][j] = f[i-1][j-1] (no op on current char)

    2. else min(f[i-1][j-1] + 1 (for replacement), f[i-1][j] + 1 (for deletion of word1[i-1]), f[i][j-1] +1 (for insertion of word2[j-1] to word1[0,...i-1])

  4. Optimization: only maintaining a row/column of the original matrix to reduce space complexity to O(n)

Code time complexity: O(n^2), space complexity: O(n^2)

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.length();
        int n = word2.length();
        // int f [m + 1][n + 1]; fill_n(f, (m+1)*(n+1), 0);
        vector<vector<int>> f (m + 1, vector<int>(n + 1, 0));
        for(int i = 1; i <= m; i++)
            f[i][0] = i;
        for(int j = 1; j <= n; j++)
            f[0][j] = j;

        for(int i = 1; i<= m; i ++){
            for(int j = 1; j <=n; j++){
                if(word1[i-1] == word2[j-1]){f[i][j] = f[i-1][j-1];}
                else { f[i][j] = min(f[i-1][j-1] + 1, min(f[i - 1][j] + 1, f[i][j-1] + 1));}
            }
        }

        return f[m][n];
    }
};

Code (with optimization) time complexity: O(n^2), space complexity: O(n)

class Solution { 
public:
    int minDistance(string word1, string word2) {
        int m = word1.length(), n = word2.length();
        vector<int> cur(m + 1, 0);
        for (int i = 1; i <= m; i++)
            cur[i] = i;
        for (int j = 1; j <= n; j++) {
            int pre = cur[0]; // pre records equivalent f[i-1][j-1]
            cur[0] = j;
            for (int i = 1; i <= m; i++) {
                int temp = cur[i]; 
                if (word1[i - 1] == word2[j - 1])
                    cur[i] = pre;
                else cur[i] = min(pre + 1, min(cur[i] + 1, cur[i - 1] + 1)); // cur[i] + 1: insertion;
                                                                             // cur[i-1] + 1: deletion;
                pre = temp; // save f[i - 1][j - 1] for the next state f[i][j]
            }
        }
        return cur[m]; 
    }
};

Special Thanks to the nice explanation by jianchao.li.fighter

results matching ""

    No results matching ""