Posted on

Description

Submission

class Solution {
public:
    bool canBeValid(string s, string locked) {
        int n = s.length();
        if(n % 2 == 1) return false;
        int maxCount = 0, minCount = 0;

        for(int i = 0; i < n; ++i) {
            if(locked[i] == '0') {
                ++maxCount;
                --minCount;
            } else if(s[i] == '(') {
                ++maxCount;
                ++minCount;
            } else {
                --maxCount;
                --minCount;
            }

            if(maxCount < 0) return false;
            if(minCount < 0) minCount = 0;
        }

        return minCount == 0;
    }
};

Leave a Reply

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