题目链接:2024. 考试的最大困扰度
仅考虑将 'F' 换成 'T' 的情况,计算在忽略 k 个字符的情况下,最多的连续 'T' 的数量即可,反之亦然。
然后返回两者的较大值。
class Solution:
def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
def max_consecutive_ch(ch: str):
l, r, ncr, ncl, ml = 0, 0, 0, 0, 0
while r < len(answerKey):
ncr += answerKey[r] != ch
r += 1
while ncr - ncl > k:
ncl += answerKey[l] != ch
l += 1
ml = max(ml, r - l)
return ml
return max(max_consecutive_ch('T'), max_consecutive_ch('F'))