-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathone_edit_distance.py
More file actions
49 lines (37 loc) · 1.11 KB
/
one_edit_distance.py
File metadata and controls
49 lines (37 loc) · 1.11 KB
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
"""
@author : imkaka
@date : 28/12/2018
# Merges the Insert/Remove and Replace Operation in One.
# Insert and Remove are Identical Operation logically.
"""
import sys
import math
def oneEditDistance(str1, str2):
if(abs(len(str1) - len(str2)) > 1):
return False
short = str1 if len(str1) < len(str2) else str2
longg = str1 if len(str1) > len(str2) else str2
id1 = 0
id2 = 0
flag = False
while(id2 < len(longg) and id1 < len(short)):
if(short[id1] != longg[id2]):
if(flag):
return False
flag = True
if(len(short) == len(longg)):
id1 += 1
else:
id1 += 1
id2 += 1
return True
def main():
strings = input().split(' ')
str1 = strings[0]
str2 = strings[1]
print(f"{str1} , {str2} : {oneEditDistance(str1, str2)}.")
print(f" {'abcd'} , {'acd'} : {oneEditDistance('abcd', 'acd')}.")
print(f"{'cake'} , {'bake'} : {oneEditDistance('cake', 'bake')}.")
print(f"{'sue'} , {'chikuu'}: {oneEditDistance('sue', 'chikuu')}.")
if __name__ == '__main__':
main()