-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathone_or_zero_edits_away.py
31 lines (28 loc) · 1.3 KB
/
one_or_zero_edits_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
def one_or_zero_edits_away(original, given):
"""
199. One Away: There are three types of edits that can be performed on strings: insert a character,
remove a character, or replace a character. Given two strings, write a function to check if they are one edit(or
zero edits) away.
"""
records_from_original = {}
leftover_from_given = []
for i in original:
records_from_original.setdefault(i, 0)
records_from_original[i] = records_from_original[i] + 1
for i in given:
if records_from_original.get(i) is None:
leftover_from_given.append(i)
else:
records_from_original[i] = records_from_original[i] - 1
print(records_from_original, leftover_from_given)
return 0 <= sum(list(records_from_original.values())) <= 1 \
and len(leftover_from_given) <= 1 \
and min(list(records_from_original.values())) >= 0
if __name__ == "__main__":
assert one_or_zero_edits_away('pale', 'ple')
assert one_or_zero_edits_away('pales', 'pale')
assert one_or_zero_edits_away('pale', 'bale')
assert one_or_zero_edits_away('pbbb', 'bbpb')
assert one_or_zero_edits_away('pale', 'bae') is False
assert one_or_zero_edits_away('pbbb', 'bpbp') is False
assert one_or_zero_edits_away('apple', 'aple')