bits made of 0 and 1 characters and an integer cap. Call a string lopsided if it has at most cap zeros **or** at most cap ones (either condition is enough).
You also get a list of queries ranges, each a pair [l, r]. For a query, take the piece bits[l..r] (both ends included) and count how many of its substrings are lopsided. Substrings at different positions count separately.
Return the answers in the same order as the queries.bits="00110", cap=1, ranges=[[0, 4], [1, 3]] → [12, 6]
The whole string has 15 substrings; "0011", "00110" and "0110" fail since each has at least two zeros and two ones, leaving 12. In the piece "011" all 6 substrings are lopsided.
bits="1", cap=1, ranges=[[0, 0]] → [1]
The only substring, "1", contains no zeros.
bits is 0 or 1bits="00110", cap=1, ranges=[[0, 4], [1, 3]]
[12, 6]
Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.