forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2024.java
57 lines (55 loc) · 1.79 KB
/
_2024.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.fishercoder.solutions;
public class _2024 {
public static class Solution1 {
public int maxConsecutiveAnswers(String answerKey, int k) {
int max;
//change T to F and count number of Fs
int right = 0;
int originalK = k;
while (k > 0 && right < answerKey.length()) {
if (answerKey.charAt(right) == 'T') {
k--;
}
right++;
}
max = right;
int left = 0;
while (right < answerKey.length() && left < answerKey.length()) {
if (answerKey.charAt(right) == 'F') {
right++;
max = Math.max(max, right - left);
} else {
while (left < right && answerKey.charAt(left) == 'F') {
left++;
}
left++;
right++;
}
}
//change F to T
right = 0;
k = originalK;
while (k > 0 && right < answerKey.length()) {
if (answerKey.charAt(right) == 'F') {
k--;
}
right++;
}
max = Math.max(max, right);
left = 0;
while (right < answerKey.length() && left < answerKey.length()) {
if (answerKey.charAt(right) == 'T') {
right++;
max = Math.max(max, right - left);
} else {
while (left < right && answerKey.charAt(left) == 'T') {
left++;
}
left++;
right++;
}
}
return max;
}
}
}