Original title Link: http://oj.leetcode.com/problems/edit-distance/
This question asks for a string to edit the minimum operand of another string, including adding, deleting, or replacing a character. The difficulty of this problem is relatively large, with conventional ideas out of the method is generally brute force, and not necessarily correct. This is actually a two-dimensional dynamic programming problem, the model is really not easy to see, the following we say recursion.
The variable Res[i][j] We maintain represents the word1 of the first I characters and the minimum number of operands edited by the first J characters of Word2. Assuming we have all the historical information before Res[i][j] to see how to get the current res[i][j in constant time, we discuss two scenarios:
1 if word1[i-1]=word2[j-1], that is, the current two characters are the same, that is, do not need to edit, then it is easy to get res[i][j]=res[i-1][j-1], because the newly added characters do not edit;
2 if word1[i-1]!=word2[j-1], then we consider three kinds of operations, if the insertion of word1, then res[i][j]=res[i-1][j]+1, that is, take word1 before i-1 characters and Word2 before the best results of J-characters, Then add an insert operation; if it is inserting word2, then the res[i][j]=res[i][j-1]+1 is the same as the previous one; if it is a substitution operation, it is similar to the first, but a replacement is added (because word1[i-1] and word2[ J-1] is not equal), so the recurrence is res[i][j]=res[i-1][j-1]+1. The list above contains all the possibilities, and a friend might say why there is no deletion, but adding an insert here can always get the same effect as a delete operation, so deleting doesn't make the least operand better, so if we are positive, we don't need to delete the operation. Take the least number of operations above, that is the result of the second case, i.e. res[i][j] = min (Res[i-1][j], res[i][j-1], res[i-1][j-1]) +1.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
The next step is to analyze the complexity of the algorithm, the time is two-tier cycle, so time complexity is O (m*n), space on each line only need one line of information, so you can use only one-dimensional array, we take M, n small and medium into the inner loop, then the complexity is O (min (m,n)). The code is as follows:
public int Mindistance (string word1, String word2) {if (Word1.length () ==0) return word2.length ();
if (Word2.length () ==0) return word1.length ();
String Minword = Word1.length () >word2.length ()? Word2:word1;
String Maxword = Word1.length () >word2.length ()? Word1:word2;
int[] res = new Int[minword.length () +1];
for (int i=0;i<=minword.length (); i++) {res[i] = i;
for (int i=0;i<maxword.length (); i++) {int[] newres = new Int[minword.length () +1];
Newres[0] = i+1;
for (int j=0;j<minword.length (); j + +) {if (Minword.charat (j) ==maxword.charat (i)) {
NEWRES[J+1]=RES[J];
else {newres[j+1] = Math.min (Res[j],math.min (res[j+1],newres[j))) +1;
} res = newres;
Return Res[minword.length ()]; }
The above code used Minword, Maxword is in order to make space is min (m,n), the details do more thin, interview time may not deliberately do so, mention it.
This topic is a relatively difficult topic, so in a short time the interview may be too tight, so there are variants. When I interviewed Google myself, I asked how I could tell if edit distance was within 1, and return True or false. Such a change in fact, there is no need for dynamic planning, only to use the distance of only 1 of this point can be judged, probably the idea is as long as there is a difference, the next can not be different, interested friends can think ha.
Author: csdn Blog Caroline-wendy