Skip to content

Instantly share code, notes, and snippets.

@arzon94
Created November 6, 2018 16:21
Show Gist options
  • Select an option

  • Save arzon94/ec7281e2a0068673c1784828df683721 to your computer and use it in GitHub Desktop.

Select an option

Save arzon94/ec7281e2a0068673c1784828df683721 to your computer and use it in GitHub Desktop.
frozenset, collections counter, dictionary methods
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