2024.cpp (471B)
1 class Solution { 2 public: 3 int maxConsecutiveAnswers(string answerKey, int k) { 4 int i = 0, j = 0, res = 0, t = 0, f = 0; 5 while (true) { 6 if (min(t, f) <= k) { 7 if (j == answerKey.size()) break; 8 (answerKey[j++] == 'T' ? t : f)++; 9 } else { 10 res = max(res, j - i); 11 (answerKey[i++] == 'T' ? t : f)--; 12 } 13 } 14 return max(res - 1, j - i); 15 } 16 };