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., setanswerKey[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:
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:
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:
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.
Constraints:
n == answerKey.length
1 <= n <= 5 * 104
answerKey[i]
is either'T'
or'F'
1 <= k <= n
Related Topics:
String, Binary Search, Sliding Window, Prefix Sum
Similar Questions:
- Longest Substring with At Most K Distinct Characters (Medium)
- Longest Repeating Character Replacement (Medium)
- Max Consecutive Ones III (Medium)
- Minimum Number of Days to Make m Bouquets (Medium)
- Longest Nice Subarray (Medium)
Check out "C++ Maximum Sliding Window Cheatsheet Template!".
Intuition: Use a sliding window to get the longest substring with at most k
'T'
(or 'F'
).
Algorithm: Implement a function count(c)
which gets the longest substring with at most k
character c
. The answer is max(count('T'), count('F'))
We can use a shrinkable sliding window:
// OJ: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int maxConsecutiveAnswers(string s, int k) {
auto count = [&](char c) {
int N = s.size(), cnt = 0, i = 0, ans = 0;
for (int j = 0; j < N; ++j) {
cnt += s[j] == c;
while (cnt > k) cnt -= s[i++] == c; // if there are more than `k` `c` characters, shrink the window until the `cnt` drops back to `k`.
ans = max(ans, j - i + 1);
}
return ans;
};
return max(count('T'), count('F'));
}
};
Or use non-shrinkable sliding window:
// OJ: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int maxConsecutiveAnswers(string s, int k) {
auto count = [&](char c) {
int N = s.size(), cnt = 0, i = 0, j = 0;
for (; j < N; ++j) {
cnt += s[j] == c;
if (cnt > k) cnt -= s[i++] == c; // If `cnt > k` we shift the window.
}
return j - i;
};
return max(count('T'), count('F'));
}
};
// OJ: https://leetcode.com/problems/maximize-the-confusion-of-an-exam
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
int maxConsecutiveAnswers(string s, int k) {
auto count = [&](char c) {
auto valid = [&](int len) {
int N = s.size(), cnt = 0;
for (int i = 0; i < N; ++i) {
cnt += s[i] != c;
if (i - len >= 0) cnt -= s[i - len] != c;
if (i >= len - 1 && cnt <= k) return true;
}
return false;
};
int L = 1, R = s.size();
while (L <= R) {
int M = (L + R) / 2;
if (valid(M)) L = M + 1;
else R = M - 1;
}
return R;
};
return max(count('T'), count('F'));
}
};