프로그래밍/HackerRank
[HackerRank][Python3] Sherlock and Anagrams
snoopybox
2018. 7. 12. 22:14
Problem :
https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem
My Solution :
#!/usr/bin/env python3
def sherlockAndAnagrams(s):
total = 0
chunk_dic = {}
for i in range(1, len(s)):
for j in range(len(s)-i+1):
chunk = ''.join(sorted(s[j:j+i]))
total += chunk_dic.setdefault(chunk, 0)
chunk_dic[chunk] += 1
return total
q = int(input())
for _ in range(q):
s = input()
result = sherlockAndAnagrams(s)
print(result)