Advertisement
Guest User

grokking_242

a guest
Nov 23rd, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | Source Code | 0 0
  1. class Solution {
  2. public int maxConsecutiveAnswers(String answerKey, int k) {
  3. return Math.max(
  4. longestSubstrAtMostKCharacter(answerKey, 'T', k),
  5. longestSubstrAtMostKCharacter(answerKey, 'F', k));
  6. }
  7.  
  8. public int longestSubstrAtMostKCharacter(String str, char target, int k) {
  9. int maxLen = 0;
  10. int left = 0;
  11. int count = 0;
  12. for (int right = 0; right < str.length(); right++) { // enlarge window
  13. if (str.charAt(right) == target) count++;
  14.  
  15. while (count > k) {
  16. // condition satisfy, shrink window
  17. if (str.charAt(left) == target) count--;
  18. left++;
  19. }
  20. maxLen = Math.max(maxLen, right - left + 1);
  21. }
  22. return maxLen;
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement