Loading...
You are given a string s made up only of the characters 0 and 1.
A substring of s is balanced if it meets all of the following:
0s as 1s.0s are next to each other, and all of its 1s are next to each other.In other words, a balanced substring is a block of 0s followed by a block of 1s of the same length, or a block of 1s followed by a block of 0s of the same length.
Return the number of balanced substrings of s.
Note that substrings starting at different indices are counted separately, even when they are made of the same characters.
Input: s = "00110011"
Output: 6
Explanation: The balanced substrings are "0011" starting at index 0, "01" at index 1, "1100" at index 2, "10" at index 3, "0011" at index 4, and "01" at index 5. The two copies of "0011" start at different indices, so both count, and the same holds for "01". The whole string "00110011" has four 0s and four 1s, but its 0s are not all next to each other, so it is not balanced.
Input: s = "10101"
Output: 4
Explanation: The balanced substrings are "10" starting at index 0, "01" at index 1, "10" at index 2, and "01" at index 3. Every substring of length 3 or more contains two equal characters with a different character between them, so none of them is balanced.
s.length ≤105s[i] is either 0 or 1.