FunctionRacerProblemsRace

Count Binary Substrings

EasyStringsPython, JavaScript, TypeScript, Java, C++

s has only 0 and 1. Count the contiguous pieces of s that have equally many 0 and 1, with all the 0 together and all the 1 together (like 0011 or 10, but not 0101). Pieces at different positions count separately.

Example 1

s "00110011"
returns 6

Six pieces: 0011, 01, 1100, 10, 0011, 01.

Example 2

s "10101"
returns 4

Four pieces of 10 or 01.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems