-
Notifications
You must be signed in to change notification settings - Fork 7
/
sherlock_and_anagrams.py
68 lines (64 loc) · 1.62 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
"""
Problem Statement
Given a string S, find the number of "unordered anagrammatic pairs" of substrings.
Input Format
First line contains T, the number of testcases. Each testcase consists of string S in one line.
Constraints
1≤T≤10
2≤length(S)≤100
String S contains only the lowercase letters of the English alphabet.
Output Format
For each testcase, print the required answer in one line.
Sample Input
2
abba
abcd
Sample Output
4
0
Explanation
Let's say S[i,j] denotes the substring Si,Si+1,⋯,Sj.
testcase 1:
For S=abba, anagrammatic pairs are: {S[1,1],S[4,4]}, {S[1,2],S[3,4]}, {S[2,2],S[3,3]} and {S[1,3],S[2,4]}.
testcase 2:
No anagrammatic pairs.
"""
def find_sub_string(string):
temp_array = []
for i in range(0, len(string)):
for j in range(i, len(string)):
temp_array.append(string[i:j+1])
return temp_array
def check_for_anagrams(string):
count = 0
substring_array = find_sub_string(string)
sorted_str_array = []
for substring in substring_array:
sorted_str_array.append(''.join(sorted(substring)))
# sorted_str_array.append(sorted(substring))
final_sorted = sorted(sorted_str_array)
seen = set()
for i in range(len(final_sorted)):
for j in range(i+1, len(final_sorted)):
if final_sorted[i] == final_sorted[j]:
count = count + 1
"""
for n in sorted_str_array:
if n in seen:
print n
count = count + 1
else:
seen.add(n)
print seen
"""
return count
if __name__ == '__main__':
n = input()
n = int(n)
string_array = []
for i in range(n):
input_string = raw_input()
string_array.append(input_string)
for i in range(n):
val = check_for_anagrams(string_array[i])
print val