• 簡單的不定長 sliding window 問題, 正反各做一遍即可。
class Solution {
private:
    int maxConsecutiveAnswersWith(string keys, int k, char c) {
        int left = 0, right = 0, res = 0, n = keys.size();
        int cnt = 0;
        while (right < n) {
            char key = keys[right++];
            while (key == c && cnt == k) {
                if (keys[left++] == c) {
                    cnt--;
                }
            } 
            if (key == c) cnt++;
            res = max(res, right-left);
        }
        return res;
    }
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        return max(maxConsecutiveAnswersWith(answerKey, k, 'T'),
                   maxConsecutiveAnswersWith(answerKey, k, 'F'));
    }
};