What is the mistake in this code?
#include<stdio.h>
#include<string.h>
int main(){
int t;
scanf("%d", &t);
while(t–){
int n;
scanf("%d", &n);
char a[n], b[n];
int i, count1 = 0, count2 = 0;
scanf("%s", a);
scanf("%s", b);
for(i = 0; i < n; i++){
if(a[i] == ‘1’)
count1++;
if(b[i] == ‘1’)
count2++;
}
if(count1 == count2)
printf(“YES\n”);
else
printf(“NO\n”);
}
return 0;
}
The problem is with char array declaration !
change char a[n]
to char *a=(char *)malloc(n*sizeof(char));
and do the same to b[n]
can you please explain why it’s so?
Hey!
You’ve forgotten to make
char a[n+1], b[n+1];
As we all know strings are terminated by ‘\0’(null character).So, you have to add an extra space for that.
Here is your AC code Hope it helps
Suggestion: Don’t just simply copy paste your code instead try to copy paste the link of your submission while posting in the community.
Make count variqble of size=2 and then,
Intialize Count 0 and 1 to zero
For(. )
{
++Count[0];
}
For(. )
{
++Count[1];
}
Now Compare both the counts.
And print
Or just sort both the string after entering and compare if its equal then identical since question only asks about possiblity🙂