My c++ 0ms solution, may help you


  • 0
    bool isValid(string s) {
        int len = s.length();
        if(len == 0) return true;
        if(len%2 != 0) return false; //exclude odd
        stack<char> store;
        for(auto c : s)
        {
            if(c=='(' || c=='[' || c=='{') 
            {
                store.push(c);
            }
            else if(!store.empty() && (c - store.top()==1 || c - store.top()==2))
            {
                store.pop();
            }
            else 
            {
                return false;
            }
        }
        return store.empty();
    }

Log in to reply
 

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