Skip to content

Commit 9d38587

Browse files
Added “Sherlock and Anagrams” solution
1 parent eb57372 commit 9d38587

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# The HackerRank Interview Preparation Kit
2+
## 3. Dictionaries and Hashmaps
3+
4+
### 3.3. Sherlock and Anagrams
5+
6+
#### Problem
7+
8+
Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. Given a string, find the number of pairs of substrings of the string that are anagrams of each other.
9+
10+
For example `s = mom`, the list of all anagrammatic pairs is `[m,m], [mo,om]` at positions [[0], [2], [[0,1], [1,2]]] recpectively.
11+
12+
<br>
13+
14+
**Function Description**
15+
16+
Complete the function `sherlockAndAnagrams` in the editor below. It must return an integer that represents the number of anagrammatic pairs of substrings in `s`.
17+
18+
`sherlockAndAnagrams` has the followint parameter(s):
19+
* `s` : a string
20+
21+
<br>
22+
23+
#### Input Format
24+
25+
The first line contains an integer `q`, the number of queries.
26+
27+
Each of the next `q` lines contains a string `s` to analyze.
28+
29+
<br>
30+
31+
#### Constraints
32+
33+
* String `s` contains only lowercase letters € ascii[a-z].
34+
* 1 <= `q` <= 10
35+
* 2 <= `|s|` <= 100
36+
37+
38+
<br>
39+
40+
#### Output Format
41+
42+
For each query, return the number of unordered anagrammatic pairs.
43+
44+
<br>
45+
46+
**Sample Input 0**
47+
48+
```
49+
2
50+
abba
51+
abcd
52+
```
53+
54+
<br>
55+
56+
**Sample Output 0**
57+
58+
```
59+
4
60+
0
61+
```
62+
63+
64+
<br>
65+
66+
**Explanation 0**
67+
68+
69+
The list of anagrammatic pairs is `[a,a]`,` [ab,ba]`, `[b,b]` and `[abb,bba]` at positions `[[0,3]]`, `[[0,1], [2,3]]`, `[[1], [2]]` and `[[0,1,2],[1,2,3]]` respectively.
70+
71+
No anagrammatic pairs exist in the second query as no character repeats.
72+
73+
74+
<br>
75+
76+
77+
**Sample Input 1**
78+
79+
```
80+
2
81+
ifailuhkqq
82+
kkkk
83+
```
84+
85+
<br>
86+
87+
**Sample Output 1**
88+
89+
```
90+
3
91+
10
92+
```
93+
94+
95+
<br>
96+
97+
**Explanation 1**
98+
99+
100+
For thw first query, we have anagram pairs `[i,i]`, `[q,q]` and `[ifa,fai]` at positions `[[0], [3]]`, `[[8],[9]]` and `[[0,1,2],[1,2,3]]` respectively.
101+
102+
For the second query:
103+
104+
There are 6 anagrams of the form `[k,k]` at positions `[[0],[1], [[0],[2]], [[0],[3]], [[1],[2]], [[1],[3]]]` and `[[2],[3]]`
105+
106+
There are 3 anagrams of the form `[kk,kk]` at positions `[[0,1], [1,2]]`, `[[0,1],[2,3]]` and `[[1,2],[2,3]]`
107+
108+
There is 1 anagram of the form `[kkk,kkk]` at positions `[[0,1,2],[1,2,3]]`
109+
110+
111+
112+
<br>
113+
114+
115+
**Sample Input 2**
116+
117+
```
118+
1
119+
cdcd
120+
```
121+
122+
<br>
123+
124+
**Sample Output 2**
125+
126+
```
127+
5
128+
```
129+
130+
131+
<br>
132+
133+
**Explanation 2**
134+
135+
136+
There are two anagrammatic pairs of lenght `1` : `[c,c]` and `[d,d]`
137+
138+
There are three anagrammatic pairs of lenght `2` : `[cd,dc]`, `[cd,cd]`, `[dc,cd]` at positions `[[0,1], [1,2]]`, `[[0,1], [2,3]]`, `[[1,2],[2,3]]` respectively.
139+
140+
141+
142+
<br>
143+
144+
145+
### Given Code
146+
147+
```python
148+
import math
149+
import os
150+
import random
151+
import re
152+
import sys
153+
154+
# Complete the sherlockAndAnagrams function below.
155+
def sherlockAndAnagrams(s):
156+
157+
if __name__ == '__main__':
158+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
159+
160+
q = int(input())
161+
162+
for q_itr in range(q):
163+
s = input()
164+
165+
result = sherlockAndAnagrams(s)
166+
167+
fptr.write(str(result) + '\n')
168+
169+
fptr.close()
170+
```
171+
172+
173+
## Solution
174+
175+
```python
176+
import math
177+
import os
178+
import random
179+
import re
180+
import sys
181+
182+
# Complete the sherlockAndAnagrams function below.
183+
def sherlockAndAnagrams(s):
184+
n = len(s)
185+
r = 0
186+
187+
for i in range(1,n):
188+
d = {}
189+
for j in range(n-i+1):
190+
subs = "".join(sorted(s[j:j+i]))
191+
if subs in d:
192+
d[subs] += 1
193+
else:
194+
d[subs] =1
195+
r += d[subs]-1
196+
197+
return r
198+
199+
200+
if __name__ == '__main__':
201+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
202+
203+
q = int(input())
204+
205+
for q_itr in range(q):
206+
s = input()
207+
208+
result = sherlockAndAnagrams(s)
209+
210+
fptr.write(str(result) + '\n')
211+
212+
fptr.close()
213+
214+
```

0 commit comments

Comments
 (0)