- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3258.java
31 lines (29 loc) · 928 Bytes
/
_3258.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
packagecom.fishercoder.solutions.fourththousand;
publicclass_3258 {
publicstaticclassSolution1 {
publicintcountKConstraintSubstrings(Strings, intk) {
intans = 0;
for (inti = 0; i < s.length(); i++) {
for (intj = i + 1; j <= s.length(); j++) {
Stringcandidate = s.substring(i, j);
if (meetConstraints(candidate, k)) {
ans++;
}
}
}
returnans;
}
privatebooleanmeetConstraints(Stringcandidate, intk) {
intones = 0;
intzeroes = 0;
for (charc : candidate.toCharArray()) {
if (c == '0') {
zeroes++;
} else {
ones++;
}
}
returnones <= k || zeroes <= k;
}
}
}