Grouped Binary Substrings
Asked at JPMorgan · Coding Questions · Difficulty: Easy · Last asked Mar 14, 2026
Given a binary string, count its non-empty substrings in which the number of `0`s equals the number of `1`s and all the `0`s are consecutive and all the `1`s are consecutive. In other words, a valid substring is a block of one character followed (or preceded) by an equally long block of the other character, such as `000111`, `111000`, `01`, or `10`. Substrings are counted by position, so equal substrings found at different positions are counted separately.
Loading the problem…