题目链接:
力扣https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/
【分析】滑动窗口的应用题
class Solution {
public int maxConsecutiveAnswers(String answerKey, int k) {
int ans = 0, right = 0, left = 0;
int n = answerKey.length();
int t = k;
char c;
while(right < n){
c = answerKey.charAt(right);
if(c == 'T'){
right++;
}else{
if(k == 0){
while(answerKey.charAt(left) == 'T'){
left++;
}
left++;
k++;
}
right++;
k--;
}
ans = Math.max(ans, right - left);
}
k = t; right = 0; left = 0;
while(right < n){
c = answerKey.charAt(right);
if(c == 'F'){
right++;
}else{
if(k == 0){
while(answerKey.charAt(left) == 'F'){
left++;
}
left++;
k++;
}
right++;
k--;
}
ans = Math.max(ans, right - left);
}
return ans;
}
}