The original title link is here: https://leetcode.com/problems/one-edit-distance/
Given strings S and T, determine if they is both one edit distance apart.
Similar to edit distance.
If the length difference is greater than 1, return false. If the length difference equals 1, when you encounter different char, the long one is moved backwards. If the lengths are equal, move backwards while encountering different char.
The loop has not returned, that is, so far the same, then see if the length difference is equal to 1. This equals 0, which means it's exactly the same or not.
Time Complexity:o (Math.min (Len1, Len2)). Space:o (1).
AC Java:
1 Public classSolution {2 Public Booleanisoneeditdistance (string s, String t) {3 if(s = =NULL|| t = =NULL){4 return false;5 }6 intLen1 =s.length ();7 intLen2 =t.length ();8 for(inti = 0; I < Math.min (len1, len2); i++){9 if(S.charat (i)! =T.charat (i)) {Ten if(Len1 = =len2) { One returnS.substring (i+1). Equals (T.substring (i+1)); A}Else if(Len1 >len2) { - returnS.substring (i+1). Equals (T.substring (i)); -}Else{ the returnS.substring (i). Equals (T.substring (i+1)); - } - } - } + returnMath.Abs (len1-len2) = = 1; - } +}
Leetcode One Edit Distance