john sokpo

Generation

generate functionFri, 07 Jul 2023

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.

class Solution { /** * @param String $answerKey * @param Integer $k * @return Integer */ function maxConsecutive($answerKey, $k) { $result = ''; if ($k >= strlen($answerKey)) { return strlen($answerKey); } $answers = str_split($answerKey); $total = count($answers); $count = 0; for($i=0; $i<$total; $i++) { if ($i == 0) { $result .= $answers[$i]; } elseif ($answers[$i] == $answers[$i-1]) { $result .= $answers[$i]; } else { if ($k > 0) { $result .= $answers[$i - 1]; $k--; } else { $result .= $answers[$i];

Want to kickstart your project?Use the new AI Studio to create your code