Note:
用双指针去维护一个滑动窗口,保证窗口里最多有K个不同的字符,若超过了,就让后面的指针往前走,划出去一个不同的字符
循环出答案
代码如下:
class Solution {
public:
int maxConsecutiveAnswers(string answerKey, int k) {
int t = maxL(answerKey, k, 'T');
int f = maxL(answerKey, k, 'F');
return max(t, f);
}
int maxL(string str, int k, char key){
int res = 0;
for(int i = 0, j = 0; j < str.size();){
while(j < str.size() && (str[j] == key || k)){
if(str[j] != key) k --;
j ++;
}
res = max(res, j - i);
while(i < str.size() && str[i ++] == key){}
if(i < str.size())
k ++;
}
return res;
}
};