Count the Number of Substrings With Dominant Ones

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
Inputs = "00011"
Output5
The substrings with dominant ones are 1 at index 3, 1 at index 4, 01, 11, and 011, for a total of 5.
Example 2
Inputs = "101101"
Output16
There are 21 substrings total and 5 of them have non-dominant ones, so there are 16 substrings with dominant ones.

Constraints

  • 1 <= s.length <= 4 * 10^4
  • s consists only of characters '0' and '1'.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate