-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp1_5.py
55 lines (41 loc) · 1.1 KB
/
p1_5.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
# Method: Char by char check, count differences
# Time: O(min(n,m)), where n,m the lenghts of the input strings
# Space: O(1)
def is_edit(a: str, b: str) -> bool:
la = len(a)
lb = len(b)
if la == lb:
return check_subst(a, b)
else:
# Make a shorter
if la > lb:
a, b = b, a
return check_edit(a, b)
def check_edit(a: str, b: str) -> bool:
la = len(a)
lb = len(b)
i = 0
j = 0
mismatches = 0
while i < la and j < lb:
if a[i] == b[j]:
i += 1
else:
mismatches += 1
j += 1
if mismatches > 1:
return False
return abs(i-j) <= 1
def check_subst(a, b) -> bool:
mistmatches = 0
for i in range(len(a)):
if a[i] != b[i]:
mistmatches += 1
if mistmatches > 1:
return False
return True
if __name__ == "__main__":
examples = [("pale", "ple"), ("pales", "pale"),
("pale", "bale"), ("pale", "bake"), ("babaqw", "babaw")]
for a, b in examples:
print(f"For {a} and {b} checking: {is_edit(a,b)}")