The shortest edit distance 72.EditDistance.md

Title Description

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:

  1. Insert a character

  2. Delete a character

  3. 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')

degree of difficulty

Hard

Ideas: as follows

First we make word1 and word2 were: michaelab and michaelxy (To understand simplicity, we assume word1 and word2 character length is the same), dis [i] [j ] as the Edit Distance between word1 and word2, we have to do michaelx is determined to minimize the steps michaely. [0]

First explain dis [i] [j]: it refers word1 [i] and word2 [j] of Edit Distance. dis [0] [0] represents word1 and word2 are empty when they are at this time of Edit Distance is 0. It is apparent drawn, dis [0] [j] is empty word1, word2 length where j, in which case they Edit Distance is j, i.e. from the air, adding converted to the j-th smallest characters Edit word2 Distance is J; Similarly dis [i] [0] is, length i word1, word2 is empty, word1 need to delete i-th character to be converted into an empty, it is converted into a minimum word2 Edit Distance is i. The following initialization code:

for (int i = 0; i < row; i++) dis[i][0] = i;
for (int j = 0; j < col; j++) dis[0][j] = j;

 

Suppose word1 [i] and word2 [j] (where i = j), respectively: michaelab and michaelxy

If b == y, dis [i]  [j] = dis [i-1] [j-1].
If b! = Y, will have to add, delete, replace, is that three operations

 

Add: y is added in a later michaelab, it becomes word1 michaelaby, this time dis [i] [j] = 1 + dis [i] [j-1];
the above formula, 1 represents just added operation, after adding operations, word1 become michaelaby, word2 is michaelxy.
dis [i] [j-1 ] from the representative conversion word1 [i] into word2 [j-1] is the minimum Edit Distance, which is converted into a minimum michaelx michaelab the
Edit Distance, since the end of the two strings y == y , so only need to michaelab become michaelx on it, and most of them between
small Edit Distance is dis [i] [j-1 ].

 

Delete: delete the latter is the michaelab b, then becomes Michaela word1, this time dis [i] [j] = 1 + dis [i-1] [j];
the above formulas, represents just delete the operation, after deletion, word1 become michaela, word2 is michaelxy. dis [i-1] [j ] from the representative
word [i-1] converted into the minimum Edit Distance word [j], which is converted into a minimum michaela Edit Distance michaelxy, so
only need to michaela it becomes michaelxy and the minimum Edit Distance between them is dis [i-1] [j ].

 

Alternatively: the latter is replaced into michaelab b y, then becomes word1 michaelay, this time dis [i] [j] = 1 + dis [i-1] [j-1];
the above formula, 1 representatives replacement operation just after the replace operation, word1 become michaelay, word2 is michaelxy. dis [i-1] [j -1] from the representative
word [i-1] converted into a word [j-1] is the minimum Edit Distance, i.e. converted into a michaelay michaelxy minimum Edit Distance, the
two strings the tail of y == y, so only need to michaela become michaelx on it, while the minimum Edit Distance between them is
dis [i-1] [j -1].

 

Program code corresponds to the following:

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.size(), n = word2.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 0; i <= m; ++i) dp[i][0] = i;
        for (int i = 0; i <= n; ++i) dp[0][i] = i;
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (word1[i - 1] == word2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
                }
            }
        }
        return dp[m][n];
    }
};

 

github Address: https://github.com/AntonioSu/leetcode/blob/master/problems/72.EditDistance.md

Reference: https://blog.csdn.net/baodream/article/details/80417695

Guess you like

Origin www.cnblogs.com/AntonioSu/p/12607379.html