-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathone_away.py
61 lines (52 loc) · 1.3 KB
/
one_away.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
def oneAwayHashed(s1, s2):
if abs(len(s1) - len(s2)) > 1:
return False
if len(s1) < 1 and len(s2) < 1:
return False
if s1 > s2:
s1, s2 = s1, s2
else:
s1, s2 = s2, s1
hash_ = [0] * (256)
for x in s1:
hash_[ord(x)] += 1
for x in s2:
hash_[ord(x)] -= 1
count_occr = 0
for x in range(256):
if hash_[x] > 0:
count_occr += 1
return count_occr <= 1
def oneAway(s1, s2):
if abs(len(s1) - len(s2)) > 1:
return False
if len(s1) < 1 and len(s2) < 1:
return False
if s1 > s2:
s1, s2 = s1, s2
else:
s1, s2 = s2, s1
m, n = len(s1), len(s2)
_found = False
i, j = 0, 0
while i<m and j<n:
if s1[i] != s2[j]:
if _found == True:
#print(s1[i], s2[j])
return False
_found = True
if m == n:
j += 1
i += 1
else:
j += 1
i += 1
return True
if __name__ == "__main__":
print(oneAwayHashed("bale", "pale"))
print(oneAwayHashed("pale", "pal"))
print(oneAwayHashed("bble", "xale"))
print("#$#$#$#")
print(oneAway("bale", "pale"))
print(oneAway("pale", "pal"))
print(oneAway("bble", "xale"))