java.lang.Object
g2001_2100.s2024_maximize_the_confusion_of_an_exam.Solution

public class Solution extends Object
2024 - Maximize the Confusion of an Exam.<p>Medium</p> <p>A teacher is writing a test with <code>n</code> true/false questions, with <code>'T'</code> denoting true and <code>'F'</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (multiple trues or multiple falses in a row).</p> <p>You are given a string <code>answerKey</code>, where <code>answerKey[i]</code> is the original answer to the <code>i<sup>th</sup></code> question. In addition, you are given an integer <code>k</code>, the maximum number of times you may perform the following operation:</p> <ul> <li>Change the answer key for any question to <code>'T'</code> or <code>'F'</code> (i.e., set <code>answerKey[i]</code> to <code>'T'</code> or <code>'F'</code>).</li> </ul> <p>Return <em>the <strong>maximum</strong> number of consecutive</em> <code>'T'</code>s or <code>'F'</code>s <em>in the answer key after performing the operation at most</em> <code>k</code> <em>times</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> answerKey = &ldquo;TTFF&rdquo;, k = 2</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> We can replace both the &rsquo;F&rsquo;s with &rsquo;T&rsquo;s to make answerKey = &ldquo;TTTT&rdquo;.</p> <p>There are four consecutive &rsquo;T&rsquo;s.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> answerKey = &ldquo;TFFT&rdquo;, k = 1</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> We can replace the first &lsquo;T&rsquo; with an &lsquo;F&rsquo; to make answerKey = &ldquo;FFFT&rdquo;.</p> <p>Alternatively, we can replace the second &lsquo;T&rsquo; with an &lsquo;F&rsquo; to make answerKey = &ldquo;TFFF&rdquo;.</p> <p>In both cases, there are three consecutive &rsquo;F&rsquo;s.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> answerKey = &ldquo;TTFTTFTT&rdquo;, k = 1</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> We can replace the first &lsquo;F&rsquo; to make answerKey = &ldquo;TTTTTFTT&rdquo;</p> <p>Alternatively, we can replace the second &lsquo;F&rsquo; to make answerKey = &ldquo;TTFTTTTT&rdquo;.</p> <p>In both cases, there are five consecutive &rsquo;T&rsquo;s.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == answerKey.length</code></li> <li><code>1 <= n <= 5 * 10<sup>4</sup></code></li> <li><code>answerKey[i]</code> is either <code>'T'</code> or <code>'F'</code></li> <li><code>1 <= k <= n</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxConsecutiveAnswers

      public int maxConsecutiveAnswers(String answerKey, int k)