One Edit Distance
Description:
Given two strings S and T, determine if they are both one edit distance apart.
Example
Given s = “aDb”, t = “adb”
return true
Code:
class Solution:
"""
@param s: a string
@param t: a string
@return: true if they are both one edit distance apart or false
"""
def isOneEditDistance(self, s, t):
# write your code here
ls = len(s)
lt = len(t)
if abs(ls-lt)>1:
return False
elif abs(ls-lt)==1:
if ls>lt:
ls, lt = lt, ls
s[:], t[:] = t[:], s[:]
for i in range(lt):
if t[0:i]+t[i+1:]==s[:]:
return True
return False
elif ls==lt:
cnt = 0
for i in range(ls):
if s[i]!=t[i]:
cnt+=1
if cnt>1:
return False
return True