Maximize the Confusion of an Exam

Medium

Description

Students answer True/False questions. You can change at most k answers. Maximize the length of consecutive same answers.

Examples

Input:answerKey = "TTFF", k = 2
Output:4
Explanation:

Change 2 F's to T's.

Input:answerKey = "TFTFTF", k = 3
Output:5
Explanation:

Change either 3 F's to T's (getting "TTTTTT" but only using 3 changes gives us "TTTFTF" with 3 consecutive T's, or "TFTFTT" with 2 consecutive T's) or 2 T's to F's (getting "FFFFFF" but only using 2 changes gives us 5 consecutive same answers). The optimal is changing 2 T's at positions 0 and 2 to get "FFFFF" for the first 5 positions.

Input:answerKey = "FFFFFFFF", k = 0
Output:8
Explanation:

When k=0, it is not possible to make any changes, so the task requires find the longest existing sequence of consecutive same answers. Since all answers are already 'F', the maximum consecutive length is the entire string length of 8.

Constraints

  • 1 ≤ answerKey.length ≤ 5 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!