Posted on

Description

Submission

class Solution {
public:
    bool checkValidString(string s) {
        int countMin = 0, countMax = 0;

        for(char ch: s) {
            if(ch == '(') {
                ++countMin;
                ++countMax;
            } else if(ch == ')') {
                --countMin;
                --countMax;
            } else {
                --countMin;
                ++countMax;
            }

            if(countMax < 0) {
                return false;
            }

            if(countMin < 0) {
                countMin = 0;   // convert to empty string
            }
        }
        return countMin == 0;
    }
};

Leave a Reply

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