Simple C++ Solution with 17 lines


  • 0
    W
    class Solution {
    public:
        bool isValid(string s) {
            if(!s.size())   return true;
            string stack;
            int i;
            for(i=0;i<s.size();++i){
                if(s[i]==')'||s[i]==']'||s[i]=='}'){
                    if(stack.back()==s[i]-1||stack.back()==s[i]-2)  stack.pop_back();
                    else    return false;
                }
                else    stack.push_back(s[i]);
            }
            if(stack.size()!=0) return false;
            return true;
        }
    };
    

Log in to reply
 

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