Count the Number of Substrings With Dominant Ones - Problem
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.
In mathematical terms, for a substring with ones count of 1s and zeros count of 0s: ones ≥ zeros²
Input & Output
Example 1 — Basic Case
$
Input:
s = "00011"
›
Output:
5
💡 Note:
Valid substrings: "1" (ones=1 ≥ zeros²=0), "11" (ones=2 ≥ zeros²=0), "1" (ones=1 ≥ zeros²=0), "01" (ones=1 ≥ zeros²=1), "011" (ones=2 ≥ zeros²=1)
Example 2 — All Ones
$
Input:
s = "101101"
›
Output:
15
💡 Note:
Most substrings work since zeros are limited: single 1s, pairs like "10", "01", and longer valid combinations
Example 3 — Many Zeros
$
Input:
s = "1000"
›
Output:
1
💡 Note:
Only "1" at the start works. Other substrings have too many zeros: "10" needs 1≥1 (ok), "100" needs 1≥4 (fail), "1000" needs 1≥9 (fail)
Constraints
- 1 ≤ s.length ≤ 4 × 104
- s consists only of characters '0' and '1'
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Binary string s with 0s and 1s
2
Check Condition
For each substring: count ones ≥ (count zeros)²
3
Count Valid
Return total number of valid substrings
Key Takeaway
🎯 Key Insight: Large zero counts make the constraint very restrictive - use this for early termination!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code