-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.cpp
More file actions
43 lines (38 loc) · 907 Bytes
/
Anagram.cpp
File metadata and controls
43 lines (38 loc) · 907 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
//Hackerrank problem:Anagram
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string.h>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
char s[10000];
cin>>t;
while(t--)
{
int ch1[26]={0},ch2[26]={0};
int i;
cin>>s;
int l=strlen(s);
if(l%2!=0)
cout<<"-1\n";
else{int count=0;
for(i=0;i<l/2;i++)
ch1[s[i]-'a']++;
for(i=l/2;i<l;i++)
ch2[s[i]-'a']++;
for(i=0;i<26;i++)
{
if(ch1[i]<=ch2[i])
continue;
else if(ch1[i]>ch2[i])
{ count+=ch1[i]-ch2[i];}
}
cout<<count<<"\n";
}
}
return 0;
}