Simple solution in c++


  • 0
    C
    class Solution {
    public:
        bool helper(char a, char b){
            return (a=='('&&b==')') || (a=='['&&b==']') || (a=='{'&&b=='}');
        }
        bool isValid(string s) {
            vector<char> vec;
            
            for (int i=0; i<s.size(); i++){
                char t = s[i];
                if(t=='(' || t=='[' || t=='{'){
                    vec.push_back(t);
                }else{
                    if(vec.size()==0) return false;
                    if (helper(vec.back(), t)){
                        vec.pop_back();
                    }else{
                        vec.push_back(t);
                    }
                }
            }
            return vec.empty();
        }
    };

Log in to reply
 

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