C++ solution using stack


  • 0
    Y
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> mystack;
            int i=0;
            string left="([{";
            string right=")]}";
            while(s[i]){
                if(left.find(s[i])!=string::npos){
                    mystack.push(s[i]);
                }
                else if(mystack.empty() || left[right.find(s[i])]!=mystack.top())
                    return false;
                else mystack.pop();
                i++;
            }
            return mystack.empty();
        }
    };
    

Log in to reply
 

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