Halo

A magic place for coding

0%

LeetCode解题报告(428)-- 72. Edit Distance

Problem

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

You have the following three operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character

Example 1:

1
2
3
4
5
6
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

1
2
3
4
5
6
7
8
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')

Constraints:

  • 0 <= word1.length, word2.length <= 500
  • word1 and word2 consist of lowercase English letters.

Analysis

  这是一道非常非常经典的动态规划题目,也是一个在工业实践中很常用的算法。首先我们来看什么是编辑距离。给定两个字符串word1word2,通过一系列的操作把word1转化为word2,操作包括在word1中插入、删除、替换。这种两个字符串变换的题目本身就很适合二维dp。dp[i][j]表示把word1i个字符转换成word2j个字符所需要的最小编辑距离。

  先来看base case。当word2长度为0时,要把word1变过去只能是删除所有的字符,所以dp[i][0] = i。同理,如果word1长度为0时,要把word1变成word2只能是添加字符,所以dp[0][j] = j

  然后来看转移方程。当word1[i - 1] == word2[j - 1]时,不需要编辑操作,所以dp[i][j] = dp[i - 1][j - 1];当word1[i - 1] != word2[j - 1]时,有三种操作:

  • word1[i - 1]替换成word2[j - 1],所以dp[i][j] = dp[i - 1][j - 1]
  • 删除掉word1[i - 1],用下一个继续和word2[j - 1]匹配,所以dp[i][j] = dp[i - 1][j]
  • word1中增加word2[j - 1],所以dp[i][j] = dp[i][j - 1].

Solution

  无


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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, 0));

for (int i = 0; i <= m; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= n; j++) {
dp[0][j] = j;
}

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], dp[i - 1][j], dp[i][j - 1]}) + 1;
}
}
}
return dp[m][n];
}
};

Summary

  这道题目是非常经典的dp,需要熟练掌握。它是很多字符串转换dp的原型,很多变形都是基于这道题目出的。这道题目的分享到这里,感谢你的支持!

Welcome to my other publishing channels