Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 3, 2019 20:02
Show Gist options
  • Select an option

  • Save imedadel/6a7cef6e95ca215054faed06520bff5f to your computer and use it in GitHub Desktop.

Select an option

Save imedadel/6a7cef6e95ca215054faed06520bff5f to your computer and use it in GitHub Desktop.
from collections import Counter
q = int(input().strip())
def anagrams(word):
count = 0
freq = Counter()
for i in range(len(word)):
for j in range(i+1, len(word)+1):
letters = list(word[i:j])
joined = "".join(sorted(letters))
if freq[joined] > 0:
count += freq[joined]
freq[joined] += 1
else:
freq[joined] += 1
return count
while(q > 0):
q -= 1
word = input().strip()
print(anagrams(word))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment