-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSherlock and Anagrams.py
50 lines (38 loc) · 1.06 KB
/
Sherlock and Anagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Problem Statement
Given a string S, find the number of unordered anagramic pairs of substrings.
"""
import collections
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
collect to map
https://github.com/derekhh/HackerRank/blob/master/sherlock-and-anagrams.cpp
:param cipher: the cipher
"""
d = collections.defaultdict(int)
lst = list(cipher)
n = len(lst)
for i in xrange(n):
for l in xrange(1, n - i + 1):
sub = lst[i: i + l]
sub.sort()
d["".join(sub)] += 1
s = 0
for v in d.values():
s += v * (v - 1) / 2
return s
if __name__ == "__main__":
import sys
f = open("0.in", "r")
# f = sys.stdin
solution = Solution()
testcases = int(f.readline().strip())
for t in xrange(testcases):
# construct cipher
cipher = f.readline().strip()
# solve
s = "%s\n" % (solution.solve(cipher))
print s,