原题链接:2024. 考试的最大困扰度
solution:
采用滑动窗口的方法解决,窗口维护的是数量较小的字符的数量长度;
class Solution {
public:
unordered_map<char,int> hash;
int maxConsecutiveAnswers(string answerKey, int k) {
int n = answerKey.size(); //滑动窗口维护的是出现次数少的那个字符的数量
int res=0;
for(int i=0,j=0;i<n;i++){
hash[answerKey[i]]++;
while(min(hash['T'],hash['F']) > k){
hash[answerKey[j]]--;
j++;
}
res = max(res,i-j+1);
}
return res;
}
};