-
Notifications
You must be signed in to change notification settings - Fork 43
/
one-edit-distance.py
209 lines (182 loc) · 5.75 KB
/
one-edit-distance.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
161. One Edit Distance
Medium
Given two strings s and t, return true if they are both one edit distance apart, otherwise return false.
A string s is said to be one distance apart from a string t if you can:
Insert exactly one character into s to get t.
Delete exactly one character from s to get t.
Replace exactly one character of s with a different character to get t.
Example 1:
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.
Example 2:
Input: s = "", t = ""
Output: false
Explanation: We cannot get t from s by only one step.
Constraints:
0 <= s.length, t.length <= 104
s and t consist of lowercase letters, uppercase letters, and digits.
"""
# V0
# IDER : RECURSION
class Solution:
def isOneEditDistance(self, s, t):
m = len(s)
n = len(t)
if abs(m - n) > 1:
return False
if m > n:
return self.isOneEditDistance(t, s)
for i in range(m):
if s[i] != t[i]:
# case 1
if m == n:
return s[i + 1:] == t[i + 1:]
# case 2
return s[i:] == t[i + 1:]
return m != n # double check this condition
# V0
# IDEA : DEAL WITH ALL CASES (Exhaustive method)
class Solution:
def isOneEditDistance(self, s, t):
lenS=len(s)
lenT=len(t)
# NOTE : the trick we deal 2 array may have different length
if lenS>lenT:
s,t=t,s
lenS,lenT=lenT,lenS
# case 1) : lenT == lenS
if lenS==lenT:
count=0
for i in range(0,lenS):
if s[i]!=t[i]:
count+=1
if count>=2:
break
if count==1:
return True
else:
return False
# case 2) : lenT - lenS >= 2
elif lenT-lenS >= 2:
return False
# case 3) lenT - lenS == 1
else:
flag=0
for i in range(0,lenT):
# if already meet "the end" of t array
if i==lenT-1 and flag==0:
return True
### NOTE : trick here,
# -> since we allow s "can change once" to see if it can qeual t
# -> when s[i-flag]!=t[i] happens first time, flag = 1
# -> when s[i-flag]!=t[i] happens AGAIN, since now flat should == 1 already, then in this case, we will return False directly
elif s[i-flag]!=t[i]:
if flag==0:
flag=1
else:
return False
return True
# V1
# IDEA : ONE PASS algorithm
# https://leetcode.com/problems/one-edit-distance/solution/
class Solution:
def isOneEditDistance(self, s: 'str', t: 'str') -> 'bool':
ns, nt = len(s), len(t)
# Ensure that s is shorter than t.
if ns > nt:
return self.isOneEditDistance(t, s)
# The strings are NOT one edit away distance
# if the length diff is more than 1.
if nt - ns > 1:
return False
for i in range(ns):
if s[i] != t[i]:
# if strings have the same length
if ns == nt:
return s[i + 1:] == t[i + 1:]
# if strings have different lengths
else:
return s[i:] == t[i + 1:]
# If there is no diffs on ns distance
# the strings are one edit away only if
# t has one more character.
return ns + 1 == nt
# V1'
# https://www.jiuzhang.com/solution/one-edit-distance/#tag-highlight-lang-python
class Solution:
# @param {string} s a string
# @param {string} t a string
# @return {boolean} true if they are both one edit distance apart or false
def isOneEditDistance(self, s, t):
# Write your code here
m = len(s)
n = len(t)
if abs(m - n) > 1:
return False
if m > n:
return self.isOneEditDistance(t, s)
for i in range(m):
if s[i] != t[i]:
if m == n:
return s[i + 1:] == t[i + 1:]
return s[i:] == t[i + 1:]
return m != n
# V1''
# https://blog.csdn.net/zhangpeterx/article/details/90577678
# https://www.cnblogs.com/lightwindy/p/8606871.html
class Solution:
def isOneEditDistance(self, s: str, t: str) -> bool:
lenS=len(s)
lenT=len(t)
if lenS>lenT:
s,t=t,s
lenS,lenT=lenT,lenS
if lenS==lenT:
count=0
for i in range(0,lenS):
if s[i]!=t[i]:
count+=1
if count>=2:
break
if count==1:
return True
else:
return False
elif lenT-lenS>=2:
return False
else:
flag=0
for i in range(0,lenT):
if i==lenT-1 and flag==0:
return True
elif s[i-flag]!=t[i]:
if flag==0:
flag=1
else:
return False
return True
# V2
# Time: O(m + n)
# Space: O(1)
class Solution(object):
def isOneEditDistance(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
m, n = len(s), len(t)
if m > n:
return self.isOneEditDistance(t, s)
if n - m > 1:
return False
i, shift = 0, n - m
while i < m and s[i] == t[i]:
i += 1
if shift == 0:
i += 1
while i < m and s[i] == t[i + shift]:
i += 1
return i == m