161. One Edit Distance
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.Input:s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.Last updated
Was this helpful?
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.Input:s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.Last updated
Was this helpful?
Was this helpful?
Input:s = "1203", t = "1213"
Output: true
Explanation: We can replace '0' with '1' to get t.class Solution(object):
def isOneEditDistance(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
m, n = len(s), len(t)
for i in range(min(m,n)):
if s[i] != t[i]:
if m > n:
return s[i+1:] == t[i:] # delete s[i]
elif m == n:
return s[i+1:] == t[i+1:] # replace
else:
return s[i:] == t[i+1:] # delete t[i]
return abs(m - n) == 1