C++, use Ascii values.


  • 1
    R
    bool isValid(string s) {
        
        stack<char> pare;
        
        for(auto c:s){
            if(c=='(' || c=='{' || c=='['){
                pare.push(c);
            }else{
                if(pare.empty()) return false;
                
                if(c-pare.top()>2) return false;
                //becasue in Ascii, (:40, ):41, [:91, ]:93, {:123, }:125
                pare.pop();
            }
        }
        return pare.empty();
    }

Log in to reply
 

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