Count Binary Substrings with Two Equal Blocks
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Count binary substrings with equal numbers of zeros and ones arranged in two consecutive blocks, including overlapping occurrences.
Read the full Microsoft Software Engineer interview experience this question came from
Constraints
- 1 <= s.length <= 100000; every character is 0 or 1.
- A valid occurrence has exactly two nonempty constant blocks of equal length, in either zero-then-one or one-then-zero order.
- Count different positions separately, even for identical text; overlapping occurrences are allowed.
- Return only the integer count. One-character and single-symbol strings return zero.
Examples
Input: ('00110011',)
Expected Output: 6
Explanation: Published sample 1: three run boundaries each contribute two occurrences.
Input: ('0101',)
Expected Output: 3
Explanation: Published sample 2: each of the three adjacent length-two occurrences qualifies.