A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).

You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:


	Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F').


Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

&nbsp;
Example 1:

Input: answerKey = "TTFF", k = 2
Output: 4
Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT".
There are four consecutive 'T's.


Example 2:

Input: answerKey = "TFFT", k = 1
Output: 3
Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
In both cases, there are three consecutive 'F's.


Example 3:

Input: answerKey = "TTFTTFTT", k = 1
Output: 5
Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT". 
In both cases, there are five consecutive 'T's.


&nbsp;
Constraints:


	n == answerKey.length
	1 &lt;= n &lt;= 5 * 104
	answerKey[i] is either 'T' or 'F'
	1 &lt;= k &lt;= n


class Solution:
    def maxConsecutiveAnswers(self, string: str, k: int) -> int:
        result = 0
        j = 0
        count1 = k
        for i in range(len(string)):
            if count1 == 0 and string[i] == "F":
                while string[j] != "F":
                    j+=1
                count1+=1
                j+=1

            if string[i] == "F":
                if count1 > 0:
                    count1-=1

            if i - j + 1 > result:
                result = i - j + 1

        j = 0
        count2 = k
        for i in range(len(string)):
            if count2 == 0 and string[i] == "T":
                while string[j] != "T":
                    j+=1
                count2+=1
                j+=1

            if string[i] == "T":
                if count2 > 0:
                    count2-=1

            if i - j + 1 > result:
                result = i - j + 1

        return result
class Solution {

    // Binary Search + Sliding Window fixed

    public int maxConsecutiveAnswers(String answerKey, int k) {

        int start = 1 ;
        int end = answerKey.length();
        int max_length = 0 ;

        while(start <= end) {
            int mid = start+(end-start)/2 ;
            if(isMax(answerKey , k , mid)) {
                max_length = mid ;
                start = mid+1 ;
            }else {
                end = mid-1 ;
            }
        }

        return max_length ;
    }

    public boolean isMax(String answerKey , int k , int max_val) {

        int T_count = 0 ;
        int F_count = 0 ;

        int i = 0 ;
        int j = 0 ;

        while(j < answerKey.length()) {

            if(answerKey.charAt(j) == 'T') {
                T_count++ ;
            }else {
                F_count++ ;
            }

            if(j-i+1 == max_val) {

                if(Math.max(T_count, F_count)+k >= max_val) {
                    return true ;
                }

                if(answerKey.charAt(i) == 'T') {
                    T_count-- ;
                }else {
                    F_count-- ;
                }

                i++ ;
            }

            j++ ;
        }

        return false ;
    }
}
class Solution {
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        return max(helper(answerKey,k,'T'),helper(answerKey,k,'F'));
    }

    int helper(string answerKey, int k,char c){
        int start = 0;
        int end = 0;
        int count = 0;
        int ans = 0;
        while(end<answerKey.length()){
            if(answerKey[end]==c)count++;
            while(count>k){
                if(answerKey[start]==c)count--;
                start++;
            }
            ans = max(ans,end-start+1);
            end++;
        }
        return ans;
    }
};
var maxConsecutiveAnswers = function(answerKey, k) {
    let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0);
    const moreChanges = () => numOfTrue > k && numOfFalse > k;
    while (right < answerKey.length) {
        if(answerKey[right] === 'T') numOfTrue++;
        if(answerKey[right] === 'F') numOfFalse++;
        while(moreChanges()) {
            if(answerKey[left] === 'T') numOfTrue--;
            if(answerKey[left] === 'F') numOfFalse--;    
            left++;    
        }
        max = Math.max(max, right - left + 1);
        right++;
    }
    return max;
};
Maximize the Confusion of an Exam