|
| 1 | +# Count the Number of Substrings With Dominant Ones (Medium) |
| 2 | + |
| 3 | +**Problem ID:** 3234 |
| 4 | +**Date:** 2025-11-15 |
| 5 | +**Link:** https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/ |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +To solve the problem of counting the number of substrings with dominant ones in a binary string, we can adopt a systematic approach that leverages the properties of the substring counts and the conditions for dominance. |
| 10 | + |
| 11 | +### Main Idea: |
| 12 | +A substring is considered to have dominant ones if the number of ones (`count_1`) is greater than or equal to the square of the number of zeros (`count_0`). This can be expressed mathematically as: |
| 13 | +\[ count_1 \geq count_0^2 \] |
| 14 | + |
| 15 | +Given this condition, we can derive a way to efficiently count valid substrings by iterating through the string and maintaining a running count of zeros and ones. |
| 16 | + |
| 17 | +### Approach: |
| 18 | +1. **Prefix Counts**: As we iterate through the string, we maintain cumulative counts of zeros and ones. This allows us to quickly calculate the number of zeros and ones in any substring defined by indices `i` and `j`. |
| 19 | + |
| 20 | +2. **Two-pointer Technique**: For each starting index `i`, we can use a second pointer `j` to extend the substring while checking the dominance condition. We will increment `j` until the condition fails, keeping track of valid substrings. |
| 21 | + |
| 22 | +3. **Count Valid Substrings**: For each valid starting index `i`, once we find the maximum index `j` where the condition holds, all substrings from `i` to any index up to `j` are valid. Thus, if `j` is the farthest index satisfying the condition, we can add `(j - i + 1)` to our count of dominant substrings. |
| 23 | + |
| 24 | +4. **Complexity Consideration**: The algorithm primarily involves iterating through the string with two pointers, leading to a time complexity of O(n), where n is the length of the string. This is efficient given the constraints. |
| 25 | + |
| 26 | +### Data Structures: |
| 27 | +- Two integers to keep track of the cumulative counts of zeros and ones. |
| 28 | +- A loop to iterate through the string, with a nested loop (or a two-pointer approach) to find valid substrings. |
| 29 | + |
| 30 | +### Complexity: |
| 31 | +- **Time Complexity**: O(n), where n is the length of the string. Each character is processed a limited number of times. |
| 32 | +- **Space Complexity**: O(1) for maintaining counts, as we only need a fixed number of variables regardless of input size. |
| 33 | + |
| 34 | +By following this structured approach, we can efficiently count the number of dominant substrings in the binary string provided. |
0 commit comments