-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.cpp
55 lines (43 loc) · 1.02 KB
/
anagram.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
52
53
54
55
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void anagram(int counterFirst[], int counterSecond[], string input)
{
if (input.length() % 2 != 0)
{
cout<<"-1"<<endl;
return;
}
for (int i = 0; i < input.length()/2; ++i)
counterFirst[input[i] - 97]++;
for (int i = input.length()/2; i < input.length(); ++i)
counterSecond[input[i] - 97]++;
int result = 0, equal = 0;
for (int i = 0; i < 26; ++i)
if (counterFirst[i] && counterSecond[i])
{
if (counterFirst[i] > counterSecond[i])
result += counterSecond[i];
else if (counterFirst[i] <= counterSecond[i])
result += counterFirst[i];
}
int x = input.length()/2 - result;
cout<<x<<endl;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
cin>>T;
while(T)
{
int counterFirst[26] = {0}, counterSecond[26] = {0};
string input;
cin >>input;
anagram(counterFirst,counterSecond, input);
T--;
}
return 0;
}