Cover image for Interview Classic 150 Questions P72 Edit Distance

Interview Classic 150 Questions P72 Edit Distance


Timeline

timeline

2025-12-16

init

dynamic programming

Title:

This problem is similar to the longest common subsequence:

Letdp[i][j]dp[i][j]denote the minimum number of operations required to convert the first i characters of word1 to the first j characters of word2.

  • Initialization:
    • word1[0..=i]Becoming an empty string requires i operations, sodp[i][0]=idp[i][0] = i
    • An empty string becomes word2[0…=j] requires j operations, sodp[0][j]=jdp[0][j] = j
  • Fordp[i][j], i,j1i,j \ge 1, there is:
    • ifword[i-1]==word[j-1], then no operation needed,dp[i][j]=dp[i1]dp[j1]dp[i][j]=dp[i-1]dp[j-1]
    • otherwisedp[i][j]=min(dp[i][j1],dp[i1][j],dp[i1][j1])+1dp[i][j] = min( dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1] ) + 1
      • dp[i1][j]+1dp[i-1][j]+1, indicates deletionword1[i-1]
      • dp[i][j1]+1dp[i][j-1]+1, indicates insertionword2[j-1]
      • dp[i1][j1]+1dp[i-1][j-1]+1, indicates replacementword1[i-1]asword2[j-1]
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;

class Solution {
public:
int minDistance(string word1, string word2)
{
// Let dp[i][j] represent the minimum number of operations required to convert the first i characters of word1 to the first j characters of word2.
int i, j;
int m = word1.length(), n = word2.length();
vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
for (i = 0; i <= m; i++) {
// Changing word1[0..=i] to an empty string requires i operations
dp[i][0] = i;
}

for (j = 0; j <= n; j++) {
// Changing an empty string to word2[0..=j] requires j operations
dp[0][j] = j;
}

for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
if (word1[i - 1] == word2[j - 1]) {
// No additional operation needed
dp[i][j] = dp[i - 1][j - 1];
} else {

// dp[i-1][j] (delete word1[i-1])
// dp[i][j-1] (insert word2[j-1])
// dp[i-1][j-1] (replace word1[i-1] with word2[j-1])

dp[i][j] = std::min({ dp[i][j - 1], dp[i - 1][j],
dp[i - 1][j - 1] }) +
1;
}
}
}
return dp[m][n];
}
};

leetcode hot 100 rewrite

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
26
27
28
29
30
31
32
33
34
35
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;

class Solution {
public:
int minDistance(string word1, string word2)
{
int i, j, n1 = word1.size(), n2 = word2.size();
// dp[i][j] represents the minimum number of steps to transform word1[0..i) into word2[0..j)
vector<vector<int> > dp(n1 + 1, vector<int>(n2 + 1, 0));

for (i = 0; i <= n1; i++)
dp[i][0] = i;

for (j = 0; j <= n2; j++)
dp[0][j] = j;

for (i = 1; i <= n1; i++) {
for (j = 1; j <= n2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = std::min({ dp[i][j - 1], dp[i - 1][j],
dp[i - 1][j - 1] }) +
1;
}
}
}
return dp[n1][n2];
}
};