Balanced Brackets(stack implementation)

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;
}

I just copy-pasted the code ,submitted it ,and it passed all test-cases in C , C++ and C++14.

Did you somehow select the wrong language ?

code is working fine.

//