Problem · String
Count Binary Substrings
Learn this problemProblem statement
Given a binary string s, return the number of nonempty contiguous substrings that contain the same number of 0 and 1 characters, with every 0 and every 1 grouped consecutively inside the substring.
Substrings are counted by their positions, so equal text occurring at different positions is counted more than once.
Function
countBinarySubstrings(s: String) → intExamples
Example 1
s = "00110011"return = 6The valid occurrences are 0011, 01, 1100, 10, 0011, and 01.
Example 2
s = "10101"return = 4Each adjacent pair forms one valid substring, and no longer substring has exactly two consecutive groups.
Constraints
1 <= s.length <= 100000.- Every character of
sis0or1.