Skip to content

Commit fb9e54f

Browse files
Create Sherlock and Anagrams
added Sherlock and Anagrams
1 parent 7b022e8 commit fb9e54f

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Sherlock and Anagrams

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
# Complete the sherlockAndAnagrams function below.
10+
def sherlockAndAnagrams(s):
11+
c=0
12+
n=len(s)
13+
d={}
14+
for i in range(n):
15+
for j in range(i+1,n+1):
16+
a="".join(sorted(s[i:j]))
17+
print(a)
18+
d[a]=d.get(a,0)+1
19+
for i in d:
20+
if(d[i]>1):
21+
c+=(d[i]*(d[i]-1))//2
22+
return(c)
23+
24+
if __name__ == '__main__':
25+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
26+
27+
q = int(input())
28+
29+
for q_itr in range(q):
30+
s = input()
31+
32+
result = sherlockAndAnagrams(s)
33+
34+
fptr.write(str(result) + '\n')
35+
36+
fptr.close()

0 commit comments

Comments
 (0)