-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOneEditDistance.py
More file actions
27 lines (25 loc) · 830 Bytes
/
OneEditDistance.py
File metadata and controls
27 lines (25 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 161. One Edit Distance
# Given two strings S and T, determine if they are both one edit distance apart.
class Solution:
def isOneEditDistance(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if not s:
return not t or len(t) == 1
if len(s) == len(t): # 替换
for i in range(len(s)):
if s[i] != t[i]:
return s[i+1:] == t[i+1:]
elif len(s) - len(t) == 1: # t中插入
for i in range(len(t)):
if s[i] != t[i]:
return s[i+1:] == t[i:]
elif len(t) - len(s) == 1: # s中插入
for i in range(len(s)):
if s[i] != t[i]:
return s[i:] == t[i+1:]
else:
return False