-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnagram.cpp
More file actions
54 lines (36 loc) · 902 Bytes
/
Anagram.cpp
File metadata and controls
54 lines (36 loc) · 902 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// Complete the anagram function below.
int anagram(string s) {
vector<int> count(26,0);
if(s.length()%2==0){
for(int i=0;i<s.size()/2;i++){
count[s[i]-'a']++;
}
for(int i=s.size()/2;i<s.size();i++){
count[s[i]-'a']--;
}
int sum = 0;
for(int i=0;i<26;i++){
sum += abs(count[i]);
}
return sum/2;
}
else
return -1;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
string s;
getline(cin, s);
int result = anagram(s);
fout << result << "\n";
}
fout.close();
return 0;
}