From https://leetcode.com/problems/one-edit-distance/
As the summary says, edit distance is an operand that makes two strings the same by transforming the characters in certain positions, and the problem is a well-known dynamic programming problem, with S and t representing two strings, S (i) representing the first I characters in S, and F (i, J) for S (i) and T (j Edit distance; Then there are the following recurrence relationships:
F (i, j) = min (
F (I-1, J) + 1; The first character of the S is deleted;
F (i, j-1) + 1; Delete the first j character of T;
F (i-1, j-1); If s (i) = = T (j)
F (i-1, j-1) + 1; S (i)! = T (j), so make a replacement;
)
So the first option is to calculate the edit distance of the given two strings, check for 1, and of course, this will time out because the time complexity of the edit distance is calculated by Dynamic programming O (M * n);
But here's the code for dynamic programming:
Public static int mindistance (STRING&NBSP;WORD1,&NBSP;STRING&NBSP;WORD2) { if (word1 == null | | word1.length () == 0) { return word2 == null ? 0 : word2.length (); } if (word2 == null | | word2.length () == 0) { return Word1.length (); } char[] ss = word1.tochararray () ; char[] ts = word2.tochararray (); int[][] m = new int[ss.length + 1][ts.length + 1]; for ( int i = 0; i < m.length; i++) { m[i][0] = i; } for (int j = 0; j < m[0].length ; j++) { m[0][j] = j; } for (int i = 1; i < m.length; i++) { for (int j = 1; j < m[i].length; j++) { int a = m[i - 1][j - 1]; if (ss[i - 1] != ts[j - 1]) { a += 1; } int b = m[i - 1][j] + 1; int c = m[i][j - 1] + 1; m[i][j] = math.min (A, math.min (b, c)); } } return m[ Ss.length][ts.length];}
In fact, the problem is to use the requirements of the edit distance to 1 of the conditions, in turn, analyze the following two strings of the case:
If the length of the two string differs by more than 1, it is certainly false, because at least two characters need to be deleted;
If the length of the two string is the same, then the characters in the two string can only have one position in each character;
If the length of the two string differs by 1, assuming that s is longer than T, then the remainder should be identical except for a position in S, when the character is deleted;
It seems that the third case is more difficult to deal with, in fact it is not so; check the characters in the S, assuming that the position I is processed, then you can know that the characters before the position I and T are the same, otherwise the flower, s and t before if there is a character different, the edit distance is at least 1. If the position s (i) = = T (i), then continue processing the next character, if s (i) = t (i), need to insert a character at the I position of T, and then check whether the remaining characters are the same; therefore, the third processing only needs to be scanned once;
Public boolean isoneeditdistance (string s, string t) { Int m = s.length (); int n = t.length (); if (m == n) { return Checksamelength (S.tochararray (), t.tochararray ()); } else if (M &NBSP;-&NBSP;N&NBSP;==&NBSP;1) { return Checkdifflength (S.tochararray (), t.tochararray ()); } else if (n &NBSP;-&NBSP;M&NBSP;==&NBSP;1) { return Checkdifflength (T.tochararray (), s.tochararray ()); } else { return false; }}private boolean Checkdifflength (char[] s, char[] t) { for (int i = 0; i < s.length; i++) { if (i == t.length) { return true; } if (S[i] == t[i]) { continue; } for (int j = i + 1; j < s.length; j++) { if (S[j] &NBSP;!=&NBSP;T[J&NBSP;-&NBSP;1]) { return false; } } return true; } return false;} Private boolean checksamelength (char[] s, char[] t) { int dist = 0; for (int i = 0; i < s.length; i++) { if (S[i] != t[i]) { dist += 1; } if (dist >&NBSP;1) { return false; } } return dist == 1;}
Determine two strings of edit distance to 1