3234. Count the Number of Substrings With Dominant Ones
Description
You are given a binary string s.
Return the number of substrings with dominant ones.
A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
Example 1:
Input: s = "00011"
Output: 5
Explanation:
The substrings with dominant ones are shown in the table below.
| i | j | s[i..j] | Number of Zeros | Number of Ones |
|---|---|---|---|---|
| 3 | 3 | 1 | 0 | 1 |
| 4 | 4 | 1 | 0 | 1 |
| 2 | 3 | 01 | 1 | 1 |
| 3 | 4 | 11 | 0 | 2 |
| 2 | 4 | 011 | 1 | 2 |
Example 2:
Input: s = "101101"
Output: 16
Explanation:
The substrings with non-dominant ones are shown in the table below.
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
| i | j | s[i..j] | Number of Zeros | Number of Ones |
|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 0 |
| 4 | 4 | 0 | 1 | 0 |
| 1 | 4 | 0110 | 2 | 2 |
| 0 | 4 | 10110 | 2 | 3 |
| 1 | 5 | 01101 | 2 | 3 |
Constraints:
1 <= s.length <= 4 * 104sconsists only of characters'0'and'1'.
Solutions
Solution 1: Preprocessing + Enumeration
According to the problem description, a dominant string satisfies \(\textit{cnt}_1 \geq \textit{cnt}_0^2\), which means the maximum value of \(\textit{cnt}_0\) does not exceed \(\sqrt{n}\), where \(n\) is the length of the string. Therefore, we can enumerate the value of \(\textit{cnt}_0\) and then calculate the number of substrings that satisfy the condition.
We first preprocess the position of the first \(0\) starting from each position in the string, storing it in the array \(\textit{nxt}\), where \(\textit{nxt}[i]\) represents the position of the first \(0\) starting from position \(i\), or \(n\) if it doesn't exist.
Next, we iterate through each position \(i\) in the string as the starting position of the substring, initializing \(\textit{cnt}_0\) to \(0\) or \(1\) (depending on whether the current position is \(0\)). Then we use a pointer \(j\) starting from position \(i\), jumping step by step to the position of the next \(0\), while updating the value of \(\textit{cnt}_0\).
For a substring starting at position \(i\) and containing \(\textit{cnt}_0\) zeros, it can contain at most \(\textit{nxt}[j + 1] - i - \textit{cnt}_0\) ones. If this quantity is greater than or equal to \(\textit{cnt}_0^2\), it means there exists a substring that satisfies the condition. At this point, we need to determine how many positions the right endpoint \(\textit{nxt}[j + 1] - 1\) can move left while still satisfying the condition. Specifically, the right endpoint has a total of \(\min(\textit{nxt}[j + 1] - j, \textit{cnt}_1 - \textit{cnt}_0^2 + 1)\) possible choices. We accumulate these counts to the answer. Then we move pointer \(j\) to the position of the next \(0\) and continue enumerating the next value of \(\textit{cnt}_0\) until \(\textit{cnt}_0^2\) exceeds the string length.
The time complexity is \(O(n \times \sqrt{n})\), and the space complexity is \(O(n)\), where \(n\) is the length of the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
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 | |
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 | |
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 | |
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 | |