Anagram
The following is the solution for Anagram in C
passed all the test cases.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int test,i,len,str1[26],str2[26];
int count;
char input[10000];
scanf("%d",&test);
while(test--)
{
scanf("%s",input);
len=strlen(input);
if(len%2!=0)
printf("-1\n");
else{
count=0;
for(i=0;i<26;i++)
{str1[i]=0;
str2[i]=0;}
for(i=0;i<len/2;i++)
str1[input[i]-97]++;
for(i=len/2;i<len;i++)
str2[input[i]-97]++;
for(i=0;i<26;i++) {
if(str1[i]>=str2[i])
count=count+(str1[i]-str2[i]);
else
count=count+(str2[i]-str1[i]);
}
printf("%d\n",count/2);
}
}
/* Enter your code here. Read
input from STDIN. Print output to STDOUT */
return 0;
}
No comments:
Post a Comment