• + 6 comments

    code in c:

    char* isBalanced(char* s) {
    int k=strlen(s);
        int *stack=malloc(sizeof(int)*k);
        int top=-1;
        for(int i=0; i<k; i++)
        {
            top++;
            stack[top]=s[i];
            if((stack[top]==')'&&(top-1>=0&&stack[top-1]=='('))||(stack[top]=='}'&&(top-1>=0&&stack[top-1]=='{'))||(stack[top]==']'&&(top-1>=0&&stack[top-1]=='[')))
                top=top-2;
        }
        if(top==-1)
            return "YES";
        else
            return "NO";
    
    }
    

    This code doesnt run in c but same code in c++ passed all test cases:

    string isBalanced(string s) {
    int k=s.size();
        int *stack=(int *)malloc(sizeof(int)*k);
        int top=-1;
        for(int i=0; i<k; i++)
        {
            top++;
            stack[top]=s[i];
            if((stack[top]==')'&&(top-1>=0&&stack[top-1]=='('))||(stack[top]=='}'&&(top-1>=0&&stack[top-1]=='{'))||(stack[top]==']'&&(top-1>=0&&stack[top-1]=='[')))
                top=top-2;
        }
        if(top==-1)
            return "YES";
        else
            return "NO";
    
    }