For "]" my machine gives false but OJ says the output is true from my code and correct answer is false.


  • 0
    R
    char stack[100000];
    int top = -1;
    void push(char c) {
        top++;
        stack[top] = c;
    }
    
    char pop() {
        char c = stack[top];
        top--;
        return c;
    }
    
    bool isValid(char* s) {
        int i;
        for(i = 0 ; s[i] ; i++) {
            if(s[i] == '(' || s[i] == '{' || s[i] == '[')
                push(s[i]);
            else if(s[i] == ')') {
                if(top == -1)
                    return false;
                if (pop() != '(')
                    return false;
            }
    
            else if(s[i] == '}') {
                if(top == -1)
                    return false;
                if (pop() != '{')
                    return false;
            }
    
            else if(s[i] == ']') {
                if(top == -1)
                    return false;
                if (pop() != '[')
                    return false;
            }
        }
        if(top == -1)
            return true;
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.