← 返回 jpmorgan 的题目列表Count Binary Substrings with Equal Consecutive Groups
类型:online_judge
Problem: Count Binary Substrings with Equal Consecutive Groups
Given a string s consisting only of '0' and '1', count the number of non-empty substrings that satisfy:
The substring contains the same number of '0' and '1'.
All '0's in the substring are consecutive and all '1's are consecutive, i.e., the substring consists of exactly two adjacent runs of characters: one run of all 0s and the other run of all 1s.
In other words, valid substrings look like 0...01...1 or 1...10...0, and the two runs must have equal length.
Input
One line: string s (only '0' and '1')
Output
An integer: the number of valid substrings
Constraints
1 <= len(s) <= 2 * 10^5
Example
s = "00110011"
Output: 6
Test Cases (5)
s = "01" → 1
s = "10" → 1
s = "0011" → 2
s = "00110011" → 6
s = "000111000" → 6
Example
Input
01
Output
1