The idea comes from: Force buckle problem solution
- First, we initialize dp[0][0]. The physical meaning is that the conversion from string '' to string '' requires a cost of 0
- Initialize the first line, physical meaning: the empty string becomes the cost of adding a, the empty string becomes the cost of adding ap... The empty string becomes the cost of adding apple
- Initialize the first column. Physical meaning: delete operation cost when string o becomes empty string, delete operation cost when string op becomes empty string... Delete operation cost when string Oppa becomes empty string
- There are two cases: word1[i-1] and word2[j-1] are equal or unequal. First discuss the inequality: as shown in the above figure, the red box, black box, blue box and yellow box.
- Red box, because o= a. Then the physical meaning of its dp value is: the minimum conversion cost for string o to string a. Its value can come from the top left, left and top. First, take the minimum of the three to operate. 1. The physical meaning of the upper left: the empty string has become an empty string after the conversion operation at the cost of 0, so the string o needs only to be updated to become string a; 2. Physical meaning on the left: o has been converted into an empty string at a cost of 1. If you want to become a string a, you only need to insert it; 3. The physical meaning above: the empty string has been converted into string a at a cost of 1. If you want to become string a, you only need to delete the character o. Finally, we choose the minimum value obtained by the three schemes as the dp value of the current red box.
- Black box, because o= p: The physical meaning of dp: the minimum conversion cost for string o to string ap. 1. The physical meaning of the upper left: the empty string has been converted into string a after the conversion operation at the cost of 1 (the same as before, that is, ao of word1 and ap of word2). If string ao wants to become string ap, it only needs to be updated; 2. Physical meaning on the left: String o has been converted into a at a cost of 1. If you want to become string ap, you only need to insert it; 3. The physical meaning above: the empty string has been converted into ap at the cost of 2. Now you only need to delete the string o. Finally, take the minimum value of the three and assign it to the black box.
- Yellow box, 1. Physical meaning at the top left: the string o has become an empty string after the conversion operation at the cost of 1, and the conversion of string p to a only needs to be replaced; 2. Physical meaning on the left: op has been changed into an empty string at a cost of 2. If you want to change it into a string a, you only need to insert it; 3. Physical meaning above: String o has been converted into string a after the conversion operation at the cost of 1. Now you only need to delete string p to get string a.
- Equal case: when you see the white box, p and p are equal, copy the dp value directly from the upper left. The physical meaning is: String o has become string a after the conversion operation at the cost of 1. Since p and p are equal, we don't need any cost to turn ap into ap.
summary
In case of inequality - top left: replace, left: insert, top: delete
class Solution { private int min(int a, int b, int c) {//Minimum value of three numbers return Math.min(a, Math.min(b, c)); } public int minDistance(String word1, String word2) { //Define the generation value of three operations int insert=1; int delete=1; int replace=1; //ranks int rows=word1.length(); int coloums=word2.length(); if(rows==0) return word2.length()*insert; if(coloums==0) return word1.length()*delete; //The dp array represents the minimum conversion cost required to convert str1[0...i-1] to str2[0...j-1] int dp[][] = new int[rows+1][coloums+1]; //Indicates that the empty string becomes an empty string after 0 times of conversion dp[0][0]=0; for (int col=1; col<=coloums; col++) { //The first line of initialization: the empty string becomes the add operation cost of a, the empty string becomes the add operation cost of ap... The empty string becomes the add operation cost of apple dp[0][col] = col*insert; } for(int row=1; row<=rows; row++) { //Initialize the first column: delete operation cost when string O becomes empty string, delete operation cost when string Op becomes empty string... Delete operation cost when string Oppa becomes empty string dp[row][0] = row*delete; } for (int row=1; row<=rows; row++) { for (int col=1; col<=coloums; col++) { if (word1.charAt(row-1) == word2.charAt(col-1)) { //The two characters are equal and copied directly from the upper left corner dp[row][col] = dp[row-1][col-1]; } else { //Assign the minimum cost of the three to dp[i][j] dp[row][col] = min(dp[row-1][col-1]+replace, dp[row-1][col]+delete, dp[row][col-1]+insert); } } } return dp[rows][coloums]; } }