2024. 考试的最大困扰度
题目链接:2024. 考试的最大困扰度
代码如下:
class Solution
{
public:
int maxConsecutiveAnswers(string answerKey, int k)
{
return max(maxConsecutiveChar(answerKey, k, 'T'),
maxConsecutiveChar(answerKey, k, 'F'));
}
int maxConsecutiveChar(const string& answerKey, int k, char ch)
{
int res = 0;
for (int left = 0, right = 0, errSum = 0; right < answerKey.size(); right++)
{
errSum += answerKey[right] != ch;
while (errSum > k)
{
errSum -= answerKey[left++] != ch;
}
res = max(res, right - left + 1);
}
return res;
}
};