Last active
March 29, 2017 17:10
-
-
Save rupalbarman/5ea70cea4a1096e19b9e0c83d5815604 to your computer and use it in GitHub Desktop.
Working on Counters (for reference)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be 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. | |
| Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number? | |
| Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings. | |
| ''' | |
| from collections import Counter | |
| c1 = Counter('baaaaccd') | |
| c2 = Counter('abbaa') | |
| print(c1, c2) | |
| print(c1- c2, c2-c1) #works like set, c1-c2 is basically set difference, which also reduces the value if same key is in both counter | |
| print((c1-c2).values()) #c1-c2 returns a counter(dict object) so values can be extracted this way | |
| print((c2-c1).values()) | |
| print(sum((c1 - c2).values()) + sum((c2 - c1).values())) #ANS sums of all values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment