forked from devAmoghS/Python-Interview-Problems-for-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_anagrams.py
36 lines (25 loc) · 894 Bytes
/
check_anagrams.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
# Problem: Two strings of sizes m and n are given,
# we have to find how many characters need to be
# removed from both the string so that they become
# anagrams of each other
import string
letters = string.ascii_lowercase
CHARACTER_HASH = dict(zip(letters, [0] * len(letters)))
def mapLettersToHash(text_a):
for char in text_a:
if char in CHARACTER_HASH.keys():
CHARACTER_HASH[char] += 1
def computeCommonLetters(text_b):
common_letters = 0
for char in text_b:
if CHARACTER_HASH[char] > 0:
common_letters += 1
return common_letters
def computeUncommonLetters(text_a, text_b, common_letters):
return abs(len(text_a) + len(text_b) - (2 * common_letters))
text_1 = "hello"
text_2 = "billion"
mapLettersToHash(text_1)
common = computeCommonLetters(text_2)
result = computeUncommonLetters(text_1, text_2, common)
print(result)