3234. Count the Number of Substrings With Dominant Ones
Difficulty: Medium
Topics: String, Sliding Window, Enumeration, Weekly Contest 408
You are given a binary string s.
Return the number of substrings1 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: 2
- 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 * 10⁴-
sconsists only of characters'0'and'1'.
Hint:
- Let us fix the starting index
lof the substring and count the number of indicesrsuch thatl <= rand the substrings[l..r]has dominant ones. - A substring with dominant ones has at most
sqrt(n)zeros. - We cannot iterate over every
rand check if thes[l..r]has dominant ones. Instead, we iterate over the nextsqrt(n)zeros to the left ofland count the number of substrings with dominant ones where the current zero is the rightmost zero of the substring.
Solution:
We need to count the number of substrings in a binary string where the number of ones is greater than or equal to the square of the number of zeros. The challenge is to efficiently compute this without checking every possible substring explicitly, which would be computationally expensive for large strings.
Approach:
Problem Analysis: The problem requires checking all possible substrings of the given binary string to determine if they meet the condition where the number of ones is at least the square of the number of zeros. Directly checking all substrings would result in an O(n^2) solution, which is inefficient for large strings (n up to 40,000).
Key Insight: For any substring to satisfy the condition, the number of zeros in the substring must be limited. Specifically, if a substring has
kzeros, it must have at leastk^2ones. Given the constraints, the maximum number of zeros in any valid substring cannot exceed a certain limit (sincek^2must be ≤ the substring length). This limit is approximately the square root of the string length, which is around 200 for n=40,000.-
Algorithm Selection: We use a sliding window approach combined with preprocessing to efficiently count valid substrings. The steps are:
- Precompute the positions of all zeros in the string.
- For each starting index
lof the substring, determine the range of ending indicesrsuch that the substrings[l..r]has at most 200 zeros and satisfies the condition. - Use the precomputed zero positions to quickly find the number of zeros in any substring and check the condition.
Complexity Analysis: The algorithm processes each starting index
land checks up to 200 possible zeros, leading to an O(200 * n) time complexity, which is efficient for the given constraints.
Let's implement this solution in PHP: 3234. Count the Number of Substrings With Dominant Ones
<?php
/**
* @param String $s
* @return Integer
*/
function numberOfSubstrings($s) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo numberOfSubstrings("00011") . "\n"; // Output: 5
echo numberOfSubstrings("101101") . "\n"; // Output: 16
?>
Explanation:
Preprocessing: We first compute an array
pre0wherepre0[i]stores the number of zeros from the start of the string up to indexi-1. We also store the positions of all zeros in the arrayzeros.-
Counting Valid Substrings: For each starting index
l, we calculate the number of valid substrings starting atl:-
Zero-length check: For substrings with zero zeros (all ones), all substrings starting at
luntil the first zero are valid. This is handled by$count += ($firstZero - $l). -
Non-zero zeros: For substrings with
kzeros (from 1 to 200), we check if the substring length is at leastk^2 + k. Using the precomputed zero positions, we determine the range ofrwhere the substrings[l..r]has exactlykzeros and meets the length condition. The count of such valid substrings is added to the total.
-
Zero-length check: For substrings with zero zeros (all ones), all substrings starting at
Efficiency: By limiting the check to at most 200 zeros per starting index, the algorithm efficiently computes the result in linear time relative to the string length, making it suitable for large inputs.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Substring: A substring is a contiguous non-empty sequence of characters within a string. ↩
Top comments (0)