JPMorgan ChaseOA #34
Count Equal Binary Substrings
Estimated EasyJudge-ready
Topics (AI-labelled)StringsTwo Pointers
Count the substrings that have the same number of 0s and 1s, with all 0s contiguous and all 1s contiguous; identical text at different positions is counted separately. 0110 has equal counts but does not qualify.
Input
The first line contains the length n, and the second line contains the binary string; the original gives no length bound, so this site uses 0≤n≤200000, and an empty string is an empty line.
Output
Output a single integer answer.
Example 1
Input:
6
011001
Output:
4
Example 2
Input:
0
Output:
0
Example 3
Input:
4
0011
Output:
2
Example notes
The outputs of the three public examples are, in order: 4; 0; 2. These answers have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…