-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramsCountSubstrings.cpp
More file actions
executable file
·44 lines (43 loc) · 1.25 KB
/
Copy pathAnagramsCountSubstrings.cpp
File metadata and controls
executable file
·44 lines (43 loc) · 1.25 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll numberOfStrings;
cin >> numberOfStrings;
for(ll i = 0; i < numberOfStrings; i++)
{
string input;
cin >> input;
unordered_map< string, ll > stringMap;
ll lengthOfString = input.size();
for(ll i = 0; i < lengthOfString; i++)
{
for(ll j = i + 1, k = 1; j <= lengthOfString; j++, k++)
{
string temp = input.substr(i, k);
sort(temp.begin(), temp.end());
unordered_map< string, ll >::iterator findIter = stringMap.find(temp);
if(findIter != stringMap.end())
{
findIter -> second += 1;
}
else
{
stringMap.insert(make_pair(temp, 1));
}
}
}
unordered_map< string, ll >::iterator iter = stringMap.begin();
ll solution = 0;
for(; iter != stringMap.end(); iter++)
{
ll value = iter -> second;
solution += ((value * (value - 1)) / 2);
}
cout << solution << endl;
}
return 0;
}