Maximize the Confusion of an Exam
MedPrompt
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., setanswerKey[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.
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.
Constraints:
n == answerKey.length1 <= n <= 5 * 104answerKey[i]is either'T'or'F'1 <= k <= n
Approaches
3 approaches with complexity analysis and trade-offs.
This approach involves checking every possible contiguous subarray within the answerKey. For each subarray, we count the number of 'T's and 'F's. If the count of either character is less than or equal to k, it means we can make the entire subarray uniform by performing at most k changes. We keep track of the maximum length of such a valid subarray.
Algorithm
- Initialize
maxLength = 0. - Use a nested loop structure. The outer loop with index
idetermines the start of a potential subarray, and the inner loop with indexjdetermines the end. - For each subarray
answerKey[i...j], count the number of 'T's (countT) and 'F's (countF). - A subarray can be made uniform if the number of characters to change is at most
k. This means eithercountT <= k(to make it all 'F's) orcountF <= k(to make it all 'T's). - If the condition
countT <= k || countF <= kis met, the current subarray of lengthj - i + 1is a valid candidate for the maximum length. - Update
maxLength = max(maxLength, j - i + 1). - After iterating through all possible subarrays, return
maxLength.
Walkthrough
We use two nested loops to define the start (i) and end (j) of each subarray. The outer loop iterates from the first character to the last to set the starting point. The inner loop expands the subarray from the starting point to the end of the string. Inside the inner loop, we maintain counts of 'T's and 'F's for the current subarray [i, j]. For each subarray, we check if it can be made entirely of 'T's (by changing at most k 'F's) or entirely of 'F's (by changing at most k 'T's). If this is possible, we update our maxLength with the current subarray's length (j - i + 1). After checking all O(N^2) subarrays, maxLength will hold the result.
class Solution { public int maxConsecutiveAnswers(String answerKey, int k) { int n = answerKey.length(); int maxLength = 0; if (k >= n / 2) { return n; } for (int i = 0; i < n; i++) { int countT = 0; int countF = 0; for (int j = i; j < n; j++) { if (answerKey.charAt(j) == 'T') { countT++; } else { countF++; } if (countT <= k || countF <= k) { maxLength = Math.max(maxLength, j - i + 1); } } } return maxLength; }}Complexity
Time
O(N^2), where N is the length of `answerKey`. The nested loops iterate through all O(N^2) subarrays.
Space
O(1), as we only use a few variables to store counts and pointers, regardless of the input size.
Trade-offs
Pros
Simple to understand and implement.
Correct for small input sizes.
Cons
Highly inefficient due to its O(N^2) time complexity.
Will result in a 'Time Limit Exceeded' (TLE) error on platforms like LeetCode for the given constraints.
Solutions
Solution
class Solution { public int maxConsecutiveAnswers ( String answerKey , int k ) { return Math . max ( get ( 'T' , k , answerKey ), get ( 'F' , k , answerKey )); } public int get ( char c , int k , String answerKey ) { int l = 0 , r = 0 ; while ( r < answerKey . length ()) { if ( answerKey . charAt ( r ++) == c ) { -- k ; } if ( k < 0 && answerKey . charAt ( l ++) == c ) { ++ k ; } } return r - l ; } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.