-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-anagram.py
More file actions
43 lines (36 loc) · 1.45 KB
/
make-anagram.py
File metadata and controls
43 lines (36 loc) · 1.45 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
# A student is taking a cryptography class and has found anagrams to be very useful.
# Two strings are anagrams of each other if the first string's letters can be rearranged to form the second string.
# In other words, both strings must contain the same exact letters in the same exact frequency.
# For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
#
# The student decides on an encryption scheme that involves two large strings.
# The encryption is dependent on the minimum number of character deletions required to make the two strings anagrams.
# Determine this number.
#
# Given two strings, A and B, that may or may not be of the same length,
# determine the minimum number of character deletions required to make A and B anagrams.
# Any characters can be deleted from either of the strings.
"""
>>> makeAnagram('cde', 'dcf')
2
>>> makeAnagram('cde', 'abc')
4
>>> makeAnagram('bacdc', 'dcbac')
0
>>> makeAnagram('bacdc', 'dcbad')
2
>>> makeAnagram('fcrxzwscanmligyxyvym', 'jxwtrhvujlmrpdoqbisbwhmgpmeoke')
30
"""
def makeAnagram(a, b):
counter = {}
incrementCounter(counter, a)
decrementCounter(counter, b)
minimumCharsToDelete = sum(abs(value) for value in counter.values())
return minimumCharsToDelete
def incrementCounter(counter, a):
for element in a:
counter[element] = counter.get(element, 0) + 1
def decrementCounter(counter, a):
for element in a:
counter[element] = counter.get(element, 0) - 1