File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // Complete the sherlockAndAnagrams function below.
6+ int sherlockAndAnagrams (string s) {
7+ int n=s.size ();
8+ int ans=0 ;
9+ for (int sz=1 ;sz<=n;sz++){
10+ for (int p=0 ; p<n-sz+1 ; p++){
11+ int occ1[26 ];
12+ memset (occ1,0 ,sizeof (26 ));
13+ for (int i=p;i<p+sz;i++)
14+ occ1[s[i]-' a' ]++;
15+ int occ2[26 ];
16+ for (int k=0 ;k<26 ;k++)
17+ occ2[k]=occ1[k];
18+ for (int j=p+sz;j<n;j++){
19+ occ2[s[j]-' a' ]++;
20+ occ2[s[j-sz]-' a' ]--;
21+ int flag=0 ;
22+ for (int k=0 ;k<26 ;k++)
23+ if (occ1[k]!=occ2[k])
24+ {flag=1 ; break ;}
25+ if (flag==0 ){
26+ ans++;
27+
28+ }
29+ }
30+ }
31+
32+ }
33+ return ans;
34+
35+ }
36+
37+ int main ()
38+ {
39+ ofstream fout (getenv (" OUTPUT_PATH" ));
40+
41+ int q;
42+ cin >> q;
43+ cin.ignore (numeric_limits<streamsize>::max (), ' \n ' );
44+
45+ for (int q_itr = 0 ; q_itr < q; q_itr++) {
46+ string s;
47+ getline (cin, s);
48+
49+ int result = sherlockAndAnagrams (s);
50+
51+ fout << result << " \n " ;
52+ }
53+
54+ fout.close ();
55+
56+ return 0 ;
57+ }
You can’t perform that action at this time.
0 commit comments