-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSherlock and Anagrams.cpp
51 lines (45 loc) · 1.1 KB
/
Sherlock and Anagrams.cpp
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
#include<iostream>
#include<cstring>
using namespace std;
int check_anagram(char sub1[], char sub2[])
{
int first[26] = {0}, second[26] = {0};
for(int c=0;sub1[c]!='\0';c++) {
first[sub1[c]-'a']++;
}
for(int c=0;sub2[c]!='\0';c++){
second[sub2[c]-'a']++;
}
for (int c = 0; c < 26; c++) {
if (first[c] != second[c])
return 0;
}
return 1;
}
int main() {
int t;
cin>>t;
while (t>0) {
char s[100];
char sub1[100] = {0};
char sub2[100] = {0};
cin>>s;
int count = 0;
for (int len = 1; len < strlen(s); len++) {
memset(sub1, 0, len);
for (int i = 0; i < strlen(s) - len; i++) {
strncpy(sub1, &s[i], len);
memset(sub2, 0, len);
for (int j = i + 1; j < strlen(s) - len + 1; j++) {
strncpy(sub2, &s[j], len);
if (check_anagram(sub1, sub2) == 1) {
count++;
}
}
}
}
cout<<count<<endl;
t--;
}
return 0;
}