-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-if-2-strings-are-one-or-two-edit-away.py
49 lines (33 loc) · 1.17 KB
/
check-if-2-strings-are-one-or-two-edit-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
"""
Check if given two strings are one edit or zero edit away.
Example:
pale, ple -> true
"""
def one_or_zero_edits_away_approach_1(s1,s2):
length_difference = abs(len(s1)-len(s2))
if length_difference>1:
# if difference is greater than 1, two strings are more than 1 edit away
return False
# both strings have same length or has one extra letter
smallest_string = s1 if len(s1)<len(s2) else s2
largest_string = s2 if len(s2)>len(s1) else s1
found_difference = 1
s_index = 0
l_index = 0
while s_index<len(smallest_string) and l_index <len(largest_string):
print(found_difference)
if smallest_string[s_index] != largest_string[l_index]:
#first difference found
if found_difference > 1:
return False
found_difference +=1
if length_difference != 0:
l_index+=1
continue
s_index+=1
l_index+=1
print(length_difference,l_index)
if length_difference == 1 and l_index<=len(largest_string)-1:
return False
return True
print(one_or_zero_edits_away_approach_1("ple","pales"))