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 mostktimes.
Input:
answerKey = "TTFF", k = 2
Output:
4
Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT".
There are four consecutive 'T's.
Example 2:
1
2
3
4
5
6
7
Input:
answerKey = "TFFT", k = 1
Output:
3
Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
In both cases, there are three consecutive 'F's.
Example 3:
1
2
3
4
5
6
7
Input:
answerKey = "TTFTTFTT", k = 1
Output:
5
Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT".
In both cases, there are five consecutive 'T's.
This problem is similar to Longest Subarray with Ones after Replacement. Since the string contains only ‘T’ and ‘F’, we can use a sliding window to find the longest substring where we can flip at most k of either character to make all characters the same. We do this for both ‘T’ and ‘F’ and take the maximum.
We want the longest substring of all ‘T’s or all ‘F’s after at most k flips. For each character (‘T’ and ‘F’), use a sliding window to keep at most k of that character in the window. If the count exceeds k, move the left pointer forward until the window is valid again. The answer is the maximum window size found for both cases.
classSolution {
public:int maxConsecutiveAnswers(string answerKey, int k) {
returnmax(solve(answerKey, k, 'T'), solve(answerKey, k, 'F'));
}
intsolve(const string& s, int k, char ch) {
int l =0, cnt =0, res =0;
for (int r =0; r < s.size(); ++r) {
if (s[r] == ch) ++cnt;
while (cnt > k) {
if (s[l++] == ch) --cnt;
}
res = max(res, r - l +1);
}
return res;
}
};
funcmaxConsecutiveAnswers(answerKeystring, kint) int {
return max(solve(answerKey, k, 'T'), solve(answerKey, k, 'F'))
}
funcsolve(sstring, kint, chbyte) int {
l, cnt, res:=0, 0, 0forr:=0; r < len(s); r++ {
ifs[r] ==ch {
cnt++ }
forcnt > k {
ifs[l] ==ch {
cnt-- }
l++ }
ifr-l+1 > res {
res = r-l+1 }
}
returnres}
func max(a, bint) int {
ifa > b {
returna }
returnb}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
publicintmaxConsecutiveAnswers(String answerKey, int k) {
return Math.max(solve(answerKey, k, 'T'), solve(answerKey, k, 'F'));
}
privateintsolve(String s, int k, char ch) {
int l = 0, cnt = 0, res = 0;
for (int r = 0; r < s.length(); r++) {
if (s.charAt(r) == ch) cnt++;
while (cnt > k) {
if (s.charAt(l++) == ch) cnt--;
}
res = Math.max(res, r - l + 1);
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funmaxConsecutiveAnswers(answerKey: String, k: Int): Int {
funsolve(s: String, k: Int, ch: Char): Int {
var l = 0var cnt = 0var res = 0for (r in s.indices) {
if (s[r] == ch) cnt++while (cnt > k) {
if (s[l++] == ch) cnt-- }
res = maxOf(res, r - l + 1)
}
return res
}
return maxOf(solve(answerKey, k, 'T'), solve(answerKey, k, 'F'))
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defmaxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
defsolve(s: str, k: int, ch: str) -> int:
l = cnt = res =0for r, c in enumerate(s):
if c == ch:
cnt +=1while cnt > k:
if s[l] == ch:
cnt -=1 l +=1 res = max(res, r - l +1)
return res
return max(solve(answerKey, k, 'T'), solve(answerKey, k, 'F'))
Instead of running two separate sliding windows, we can use a frequency map to track the count of each character (‘T’ and ‘F’) in the current window. The window is valid as long as the number of replacements needed (window size minus the count of the most frequent character) does not exceed k. This allows us to find the longest valid window in a single pass.