We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
from collections import Counter
def sherlockAndAnagrams(s):
anagrams = 0
c = Counter()
for i in range(len(s)):
combos = ["".join(sorted(s[j:j+i+1])) for j in range(len(s) - i)]
c.update(combos)
for k in c:
anagrams += (c[k] - 1) * c[k] // 2
return anagrams
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
I modified you solution to use Counter: