Count Binary Substrings

EasyStringstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a binary string s, count its non-empty substrings that consist of some number of 0s and the **same** number of 1s, where all the 0s are consecutive and all the 1s are consecutive (for example "0011" or "10", but not "0101"). Substrings that occur at different positions are counted separately, even if they are equal as strings.

Examples

Input: s = "00110011"
Output: 6
The qualifying substrings are "0011", "01", "1100", "10", "0011", and "01".
Input: s = "10101"
Output: 4
"10", "01", "10", "01".

Constraints

  • 1 <= s.length <= 10^5
  • `s[i]` is either `"0"` or `"1"`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems