I wrote this code for the problem Balanced Brackets but I can’t seem to know why it gives segmentation fault in some test cases.
char* isBalanced(char* s)
{
// Complete this function
char* stack = (char *)malloc(5000 * sizeof(char));
int t=-1,i;
for(i=0; s[i] !='\0'; i++)
{
if(t==-1)
{
stack[++t]=s[i];
}
else
{
if(stack[t]==s[i]-2 || stack[t]==s[i]-1)
{
t--;
}
else
{
stack[++t]=s[i];
}
}
}
char *yes="YES";
char *no="NO";
if(t==-1)
return yes;
else
return no;
}
int main()
{
int t;
scanf("%i", &t);
for(int a0 = 0; a0 < t; a0++)
{
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s", s);
int result_size;
char* result = isBalanced(s);
printf("%s\n", result);
}
return 0;
}