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.
fromcollectionsimportCounterfromitertoolsimportcombinationsdefsherlockAndAnagrams(s):substrings=[''.join(sorted(s[i:j]))fori,jincombinations(range(len(s)+1),2)]substring_counts=Counter(substrings)returnsum([v*(v-1)//2 for _,v in substring_counts.items()])
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 →
You can also use itertools combinations: