Simple C++ 0ms solution


  • 0
    U
    class Solution {
    public:
        bool isValid(string s) {
            map<char,int> M = {{'(',-1},{')',1},{'{',-2},{'}',2},{'[',-3},{']',3}};
            stack<int> S;
            S.push(0);
            for(auto ch : s)
            {
                if(M[ch] + S.top() == 0 && S.top() < 0)
                    S.pop();
                else if(M[ch]*S.top() < 0)
                    return false;
                else S.push(M[ch]);
            }
            return S.top() == 0;
        }
    };

Log in to reply
 

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