Posted on

Description

Submission

class Solution {
public:
    bool isValid(string s) {
        string stk = "";
        
        for(auto ch: s) {
            if(stk.size() < 2 || ch == 'a' || ch == 'b') {
                stk.push_back(ch);
                continue;
            }
            // if 'c'
            if(stk.back() == 'b') {
                stk.pop_back();
                if(stk.back() == 'a') {
                    stk.pop_back();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        if(!stk.empty()) return false;
        return true;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *