Maximize the Confusion of an Exam

Given a string answerKey consisting of 'T's and 'F's representing the original answers to a series of true/false questions, and an integer k representing the maximum number of times you can change an answer, determine the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.


Return this maximum number of consecutive 'T's or 'F's.


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.



Sliding window technique:

1. Initialize variables `right` and `left` to keep track of the sliding window, `n` to store the length of the answer key, and `maxLength` to store the maximum length of consecutive 'T's or 'F's.

2. Create a HashMap `map` to keep track of the count of 'T's and 'F's in the current window.

3. Iterate through the answer key using a sliding window approach, moving `right` pointer to the right.

4. Update the count of 'T's or 'F's in the window by putting the current character into the map and updating its count.

5. While the count of 'T's and 'F's in the window both exceed `k`, move the `left` pointer to the right and update the map accordingly to shrink the window until the counts satisfy the condition.

6. Check if the count of 'T's or 'F's in the window is less than or equal to `k`. If so, update `maxLength` by comparing it with the current window length (`right - left + 1`).

7. Repeat steps 3-6 until reaching the end of the answer key.

8. Return `maxLength`, which represents the maximum number of consecutive 'T's or 'F's with at most `k` changes.




Java implementation:

    public int maxConsecutiveAnswers(String answerKey, int k) {
        int right =0;
        int left =0;
        int n = answerKey.length();
        Map<Character,Integer> map = new HashMap<>();
        int maxLength = 0;

        while(right<n){
            map.put(answerKey.charAt(right),map.getOrDefault(answerKey.charAt(right),0)+1);
            
            while(map.getOrDefault('T',0)>k && map.getOrDefault('F',0)>k){
                if(map.getOrDefault(answerKey.charAt(left),0)== 1 ){
                    map.remove(answerKey.charAt(left));
                }else{
                    map.put(answerKey.charAt(left),map.getOrDefault(answerKey.charAt(left),0)-1); 
                }
                left++;
            }
            if( map.getOrDefault('T',0)<=k || map.getOrDefault('F',0)<=k){
                maxLength = Math.max(maxLength,right-left+1);
            }

            right++;
        }
        return maxLength;
    }




C++ implementation:

#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

int maxConsecutiveAnswers(string answerKey, int k) {
    int right = 0;
    int left = 0;
    int n = answerKey.length();
    unordered_map<char, int> map;
    int maxLength = 0;

    while (right < n) {
        map[answerKey[right]]++;

        while (map['T'] > k && map['F'] > k) {
            if (map[answerKey[left]] == 1) {
                map.erase(answerKey[left]);
            } else {
                map[answerKey[left]]--;
            }
            left++;
        }
        
        if (map['T'] <= k || map['F'] <= k) {
            maxLength = max(maxLength, right - left + 1);
        }

        right++;
    }

    return maxLength;
}






Python implementation:

def maxConsecutiveAnswers(answerKey: str, k: int) -> int:
    right = 0
    left = 0
    n = len(answerKey)
    map = {}
    maxLength = 0

    while right < n:
        map[answerKey[right]] = map.get(answerKey[right], 0) + 1

        while map.get('T', 0) > k and map.get('F', 0) > k:
            if map.get(answerKey[left], 0) == 1:
                del map[answerKey[left]]
            else:
                map[answerKey[left]] -= 1
            left += 1

        if map.get('T', 0) <= k or map.get('F', 0) <= k:
            maxLength = max(maxLength, right - left + 1)

        right += 1

    return maxLength

No comments

darkmode