Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int maxConsecutiveAnswers(String answerKey, int k) {
- return Math.max(
- longestSubstrAtMostKCharacter(answerKey, 'T', k),
- longestSubstrAtMostKCharacter(answerKey, 'F', k));
- }
- public int longestSubstrAtMostKCharacter(String str, char target, int k) {
- int maxLen = 0;
- int left = 0;
- int count = 0;
- for (int right = 0; right < str.length(); right++) { // enlarge window
- if (str.charAt(right) == target) count++;
- while (count > k) {
- // condition satisfy, shrink window
- if (str.charAt(left) == target) count--;
- left++;
- }
- maxLen = Math.max(maxLen, right - left + 1);
- }
- return maxLen;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement