-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily156.java
More file actions
32 lines (26 loc) · 788 Bytes
/
Copy pathdaily156.java
File metadata and controls
32 lines (26 loc) · 788 Bytes
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
// Solution 1
// Solution 2
class Solution {
public int takeCharacters(String s, int k) {
// Total counts
int[] count = new int[3];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
if (Math.min(Math.min(count[0], count[1]), count[2]) < k) {
return -1;
}
// Sliding Window
int res = Integer.MAX_VALUE;
int l = 0;
for (int r = 0; r < s.length(); r++) {
count[s.charAt(r) - 'a']--;
while (Math.min(Math.min(count[0], count[1]), count[2]) < k) {
count[s.charAt(l) - 'a']++;
l++;
}
res = Math.min(res, s.length() - (r - l + 1));
}
return res;
}
}