-
Notifications
You must be signed in to change notification settings - Fork 0
/
sherlock_and_anagrams.py
69 lines (55 loc) · 1.61 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'sherlockAndAnagrams' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#
def sortWord(word):
sorted_string_array = sorted(word)
return ''.join(sorted_string_array)
def produceSubstrings(s):
substring_list = []
n = len(s)
for i in range(0, n - 1):
for j in range(0, n):
substring = s[j:j+i+1]
if len(substring) == i+1:
substring_list.append(substring)
return substring_list
def identifyAnagrams(substrings):
dict_count = {}
for sub in substrings:
sub = sortWord(sub)
if dict_count.get(sub) is None:
dict_count[sub] = 1
else:
dict_count[sub] += 1
return dict_count
def computeCombinations(n, r):
return int((n * (n - 1)) / r)
def countAnagramPairs(anagrams):
count_anagrams = 0
for anagram in anagrams.keys():
number_of_anagrams = anagrams.get(anagram)
if number_of_anagrams > 1:
number_of_comb = computeCombinations(number_of_anagrams, 2)
count_anagrams += number_of_comb
return count_anagrams
def sherlockAndAnagrams(s):
substrings = produceSubstrings(s)
anagrams = identifyAnagrams(substrings)
return countAnagramPairs(anagrams)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
for q_itr in range(q):
s = input()
result = sherlockAndAnagrams(s)
fptr.write(str(result) + '\n')
fptr.close()