C++ easy to follow solution with stack.


  • 0
    C
    bool isValid(string s) {
        stack<char> myStack;
        string left="([{", right=")]}";
        for (int i=0; i < s.size(); i++) {
            if (left.find(s[i])!=string::npos)  
                myStack.push(s[i]);
            else
                if (myStack.size()!=0 && left.find(myStack.top())==right.find(s[i]))
                    myStack.pop();
                else
                    return false;
        }
        return myStack.size()==0;
    }

Log in to reply
 

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