Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-139

Count Binary Substrings

Problem

Count substrings of s that have an equal number of consecutive 0s and consecutive 1s, grouped together as in 0011 or 10.

Track the length of the current run and the previous run; each position contributes min(previous, current) valid substrings. Assume s contains only 0 and 1 characters.

Hint

Only boundaries between a run and the next run can produce valid substrings.

Test cases

case 1
in: ["00110011"]
expected: 6
case 2
in: ["10101"]
expected: 4
case 3
in: [""]
expected: 0
case 4
in: ["1"]
expected: 0
case 5
in: ["000111"]
expected: 3
Starter codePython
def count_binary_substrings(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor