Edit Distance

Problem Statement

Given two words word1 and word2, 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

This problem may be treated as an opertation problem. But in fact it's an counting problem about optimizing. That's why we'll consider using DP to solve it.

Define $dp[i][j] := $ minimum number of steps convert word1[1:i] to word2[1:j].

The status transform equation.

In process of converting word1[$1:i$] to word2[$1:j$], the minimum solution must be one of the three converting method:

  1. At the last step, the operation is exactly converting character word1[$i$] into character word2[$j$], i.e. word1 and word2 terminate at the same time. So the transform equation should be:$dp[i][j] = dp[i-1][j-1] +$ last step cost.
  2. Before reaching the end of word1, word1's beginning part has been converted into word2, i.e. word2 terminates in advance.  So at the last step, the operation must be deleting the last character word1[i] where word1[1:(i-1)] has been converted into word2[1:j] in optimized way. So the equation should be:
    $dp[i][j] = dp[i-1][j] + 1$.
  3. Before reaching the end of word2, word1 has been converted into word2's beginning part, i.e. word1 terminates in advance. So the last step must be inserting the last character word2[j] into word1 where word1[1:i] has been converted into word2[1:(j-1)] in optimized way. So the equation should be
    $dp[i][j] = dp[i][j-1] + 1$.

So the minimum steps of dp[i][j] is the minimum steps of above three statuses. One more thing need to notice is that, at first status, the last step cost depends on if word[i] is equal to word[j]. If it is, last step cost equals 0, else last step cost equals 1.

 

Coclude the above discussion as:

lstCost = (word[i] == word[j]) ? 0 : 1;
dp[i][j] = min(dp[i-1][j-1] + lstCost, dp[i-1][j] + 1, dp[i][j-1] + 1);

 

The boundary condition is simple, just as

  1. convert from "" to "": $dp[0][0] = 0$;
  2. convert from "" to word2[1:j]: $dp[0][j] = j$; Here, $j \geq 1.$
  3. convert from word1[1:i] to "": $dp[i][0] = i$; Here, $i \geq 1.$

 

The corresponding code should be:

dp[0][0] = 0;
for(int i = 1; i <= word1.length(); ++i)
    dp[i][0] = i;
    
for(int j = 1; j <= word2.length(); ++j)
    dp[0][j] = j;

 


The complete code is:

class Solution {
public:
    int minDistance(string word1, string word2) {
        int i = 0;
        int steps = 0;
        
        while(word1[i] != 0 && word2[i] != 0){
            if(word1[i] != word2[i]){
                word1[i] = word2[i];
                ++steps;
            }
            ++i;
        }
        
        if(word1[i] != 0){
            steps += word1.end() - word1.begin() - i;
            word1.erase(word1.begin()+i, word1.end());
        }
        
        if(word2[i] != 0){
            word1 += word2[i];
            ++i;
            ++steps;
        }
        
        return steps;
    }
};

 

posted @ 2014-11-30 09:57  kid551  阅读(172)  评论(0编辑  收藏  举报