Hackerrank Anagram solution
The below program is the solution of Anagram in implementation sub-domain. You can refer the question from HERE.
Output :
Code :
#include<stdio.h> int main() { int t,i,x,c,flag; char s[10001]; int *a=(int*)malloc(sizeof(int)*26); int *b=(int*)malloc(sizeof(int)*26); memset(a,0,sizeof(int)*26); memset(b,0,sizeof(int)*26); scanf("%d",&t); while(t--) { scanf("%s",&s); flag=0;c=0; if(strlen(s)%2==1) { printf("-1\n"); continue; } for(i=0;i<26;i++) { a[i]=b[i]=0; } for(i=0;i<strlen(s)/2;i++) a[s[i]-'a']++; for(i=strlen(s)/2;i<strlen(s);i++) b[s[i]-'a']++; for(i=0;i<26;i++) { c+=abs(a[i]-b[i]); } printf("%d\n",c/2); } }
Comments
Post a Comment