161. One Edit Distance
Given two stringss andt, determine if they are both one edit distance apart.
Note:
There are 3 possiblities to satisify one edit distance apart:
Insert a character into s to get t
Delete a character from s _to get t_
Replace a character of s _to get t_
Example 1:
Example 2:
Example 3:
Thoughts:
Compare the string until first no equal char meet:
return a[i+1:] == b[i:] # for deleteing ith element in a (assume a is longer than b)
return a[i+1:] == b[i+1] # for replacing one to the other
if two strings are equal in min(len(a),len(b)) return whether the distance is within 1 # for insert shorter one the last element from the longer one at the end
Code
Last updated
Was this helpful?