-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3234.java
43 lines (42 loc) · 1.89 KB
/
_3234.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
package com.fishercoder.solutions.fourththousand;
public class _3234 {
public static class Solution1 {
/*
* Sliding window.
* credit: https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/solutions/5547005/sliding-window-java-o-sqrt-of-n-n/
* The idea is:
* 1. we fix the number of zeroes in each iteration, then the number of ones is zeroes * zeroes;
* 2. now we operate the sliding window.
*/
public int numberOfSubstrings(String s) {
int ans = 0;
for (int zeroes = 0; zeroes * zeroes < s.length(); zeroes++) {
int[] count = new int[2];
int lastPos = -1;
// end keeps moving to the right in each iteration
for (int start = 0, end = 0; end < s.length(); end++) {
count[s.charAt(end) - '0']++;
while (start < end) {
if (s.charAt(start) == '0' && count[0] > zeroes) {
// this means we have more zeroes than we want, so we'll move start to
// the right by one
count[0]--;
lastPos = start;
} else if (s.charAt(start) == '1' && (count[1] - 1) >= (zeroes * zeroes)) {
// this means the current start position is '1' and after excluding it,
// the window is still a valid dominant one
count[1]--;
} else {
break;
}
start++;
}
if (count[0] == zeroes && count[1] >= zeroes * zeroes) {
ans += (start - lastPos);
}
}
}
return ans;
}
}
}