# 72. Edit distance

Title Description

Here are two words word1 and word2. Please calculate the minimum operands used to convert word1 to word2.
You can perform the following three operations on a word:

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

Tips:

• 0 < = w o r d 1. l e n g t h , w o r d 2. l e n g t h < = 500 0 <= word1.length, word2.length <= 500 0<=word1.length,word2.length<=500
• word1 and word2 are composed of lowercase English letters The question is 583. Deleting two strings The upgraded version of is the ultimate version of editing distance (can be added / deleted / modified at the same time).

## dp five steps

1. dp[i][j] indicates the minimum operand to be used when word1[0,i-1] and word2[0,j-1] become the same string
In this way, it is not necessary to use recurrence formula to deal with dp[i] and dp[j].
If dp[m][n] is defined, the recursive formula should be used separately to process dp[i] and dp[j].
2. Recurrence formula
It is still divided into two categories, that is, the current characters are equal and the current characters are not equal:
• If the current characters are equal, no operation is required, dp[i][j] = dp[i - 1][j - 1];
• The current characters are not equal, which can be divided into three cases (i.e., add, delete and modify):
• (a) Delete word1[i-1], i.e. dp[i - 1][j] + 1;
• (b) Delete word2[j-1], i.e. dp[i][j - 1] + 1;
• (c) Modify word1[i-1] or word2[j - 1] (no matter who modifies them, the minimum operand required is the same), that is, dp[i - 1][j - 1] + 1;
• The third is min.

Here are two questions: 🔥

1. Why is there no operation to insert characters? ⭐ ️
A: deleting the current character word1[i - j] in word1 is equivalent to adding a character in word2. The minimum operands required for both are the same. For example, word1 = "ad", word2 = "a". One operation is required for wrod1 to delete d and word2 to add d.
2. Why can't the current characters word1[i-1] and word2[j-1] be deleted at the same time? ⭐ ️
A: because the minimum operand is required, two operands are required to delete the current characters of both, while only one operand is required to replace one of the characters. That is, replacing one of the two characters is less than deleting two characters at the same time.
1. initialization
If word1[0, i-1] is equal to the empty string word2, i characters need to be deleted;
If word2[0, j-1] is equal to the empty string word1, j characters need to be deleted
```// If word1[0, i-1] is equal to the empty string word2, i characters need to be deleted
for (int i = 0; i <= m; i++) dp[i] = i;
for (int j = 0; j <= n; j++) dp[j] = j; // Similarly
```
2. traversal order
According to the recurrence formula, the current state dp[i][j] needs to depend on the left, top and top left States, so it needs to be from top to bottom and from left to right
3. Print dp
```class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m + 1][n + 1]; // dp[i][j] indicates the minimum operand to be used when word1[0,i-1] and word2[0,j-1] become the same string
// 3. Initialization
// If word1[0, i-1] is equal to the empty string word2, i characters need to be deleted
for (int i = 0; i <= m; i++) dp[i] = i;
for (int j = 0; j <= n; j++) dp[j] = j; // Similarly
// 4. Traversal order (according to the recursive formula, the current state dp[i][j] needs to depend on the left, top and top left States, so it needs to be from top to bottom and from left to right)
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// 2. Recurrence formula
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
// The current characters are equal and no action is required
dp[i][j] = dp[i - 1][j - 1];
} else {
// The current characters are not equal, which can be divided into three cases (i.e., add, delete and modify):
// (1) Delete word1[i-1], i.e. dp[i - 1][j] + 1;
// (2) Delete word2[j-1], i.e. dp[i][j - 1] + 1;
// (3) Modify word1[i-1] or word2[j - 1] (no matter who modifies them, the minimum operand required is the same), that is, dp[i - 1][j - 1] + 1;
// The third is min.
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
/**
Here are two questions:
1. Why is there no operation to insert characters?
A: deleting the current character word1[i - j] in word1 is equivalent to adding a character in word2. The minimum operands required for both are the same. For example, word1="ad",word2="a". One operation is required for wrod1 to delete d and word2 to add d
2. Why can't the current characters word1[i-1] and word2[j-1] be deleted at the same time?
A: because the minimum operand is required, two operands are required to delete the current characters of both, while only one operand is required to replace one of the characters. That is, replacing one of the two characters is less than deleting two characters at the same time.
*/
}
}
}
// 5. Print dp
for (int i = 0; i <= m; i++) {
System.out.println(Arrays.toString(dp[i]));
}
return dp[m][n];
}
}
``` Posted on Tue, 26 Oct 2021 21:00:39 -0400 by ksp