Sherlock and Anagrams

  • + 9 comments

    yes you're right, we don't need to list all combinations to count pairs. using your formula -

    from collections import Counter
    def sherlockAndAnagrams(s):
        count = 0
        for i in range(1,len(s)+1):
            a = ["".join(sorted(s[j:j+i])) for j in range(len(s)-i+1)]
            b = Counter(a)
            for j in b:
                count+=b[j]*(b[j]-1)/2
        return int(count)