
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let us fix the starting index <code>l</code> of the substring and count the number of indices <code>r</code> such that <code>l <= r</code> and the substring <code>s[l..r]</code> has dominant ones.
A substring with dominant ones has at most <code>sqrt(n)</code> zeros.
We cannot iterate over every <code>r</code> and check if the <code>s[l..r]</code> has dominant ones. Instead, we iterate over the next <code>sqrt(n)</code> zeros to the left of <code>l</code> and count the number of substrings with dominant ones where the current zero is the rightmost zero of the substring.