Created
November 6, 2018 16:21
-
-
Save arzon94/ec7281e2a0068673c1784828df683721 to your computer and use it in GitHub Desktop.
frozenset, collections counter, dictionary methods
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
| from collections import Counter | |
| # Complete the sherlockAndAnagrams function below. | |
| def sherlockAndAnagrams(s): | |
| count = 0 | |
| anagramString = s | |
| dict1 = {} | |
| for i in range(len(s)): | |
| for j in range(1, len(s) - i + 1): | |
| key = frozenset(Counter(s[i:i+j]).items()) | |
| dict1[key] = dict1.get(key, 0) + 1 | |
| for key in dict1: | |
| count += dict1[key] * (dict1[key] - 1) // 2 | |
| return count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment