Maximize the Confusion of an Exam - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
answerKey = "TTFF", k = 2
›
Output:
4
💡 Note:
We can change at most 2 characters. Change both F's to T's to get "TTTT" with length 4, or change both T's to F's to get "FFFF" with length 4.
Example 2 — Mixed Pattern
$
Input:
answerKey = "TFFT", k = 1
›
Output:
3
💡 Note:
We can change 1 character. Change the middle F to T to get "TTFT" or "TFTT", giving us a maximum consecutive length of 3 T's.
Example 3 — No Changes Needed
$
Input:
answerKey = "TTTTTTTT", k = 2
›
Output:
8
💡 Note:
All characters are already the same, so we don't need any changes. The entire string has length 8.
Constraints
- 1 ≤ answerKey.length ≤ 5 × 104
- answerKey[i] is either 'T' or 'F'
- 0 ≤ k ≤ answerKey.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with T's and F's, plus change limit k
2
Process
Find longest substring by changing ≤ k characters
3
Output
Maximum length of consecutive same characters
Key Takeaway
🎯 Key Insight: Use sliding window to maintain at most k changes while maximizing consecutive characters
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code