Maximize the Confusion of an Exam

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.

You are given a string answerKey, where answerKey[i] is the original answer to the i^th 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.

Example 1
InputanswerKey = "TTFF", k = 2
Output4
We can replace both the 'F's with 'T's to make answerKey = "TTTT", giving four consecutive 'T's.
Example 2
InputanswerKey = "TFFT", k = 1
Output3
We can replace either the first or second 'T' with an 'F', giving three consecutive 'F's.

Constraints

  • n == answerKey.length
  • 1 <= n <= 5 * 10^4
  • answerKey[i] is either 'T' or 'F'
  • 1 <= k <= n

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate