-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnagram.cpp
More file actions
48 lines (36 loc) · 843 Bytes
/
Anagram.cpp
File metadata and controls
48 lines (36 loc) · 843 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
// problem: "https://www.hackerrank.com/challenges/anagram/problem"
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int T,i,j,count,str1[26],str2[26];
char str[10000];
cin>>T;
for(int k=0;k<T;k++)
{
cin>>str;
for(i=0;i<26;i++)
str1[i]=str2[i]=0;
if(strlen(str)%2!=0)
cout<<-1<<endl;
else
{
i=0;
while(i<(strlen(str))/2)
{
str1[(int)(str[i])-97]++;
str2[(int)(str[i+((strlen(str))/2)])-97]++;
i++;
}
count=0;
for(j=0;j<26;j++)
{
if(str2[j]>=str1[j])
count=count+str2[j]-str1[j];
}
cout<<count<<endl;
}
}
return 0;
}