Count Balanced Two-Block Binary Substrings
StringsAuto-graded · 24 test casesLast updated
Count substrings of a binary string made of one block of equal characters followed by a block of the other character, with both blocks the same length.
You are given a binary string s that contains only the characters '0' and '1'.
Count the non-empty substrings of s that have this shape: a block of identical characters immediately followed by a block of the opposite character, where both blocks have the same length. Equivalently, a qualifying substring is k copies of one digit followed by k copies of the other digit, for some k >= 1. Examples of the shape are "01", "10", "0011", and "111000".
A substring such as "0101" does not qualify. Its counts are equal, but the zeros and ones are interleaved instead of forming two blocks.
Substrings are counted by position. If the same text appears at several different positions in s, each position counts separately.
Return the total number of qualifying substrings. If s is empty or has no qualifying substring, return 0.
Constraints
0 <= len(s) <= 10^5scontains only the characters'0'and'1'.- The function returns a single non-negative integer.
Examples
Example 1
Input: count_two_block_substrings('0011')
Output: 2
Example 2
Input: count_two_block_substrings('10101')
Output: 4
Example 3
Input: count_two_block_substrings('00110011')
Output: 6
Example 4
Input: count_two_block_substrings('0000')
Output: 0
Hints
Hint 1
Look at the string as a sequence of maximal runs of the same character. What can a qualifying substring look like in terms of those runs?
Hint 2
Every qualifying substring sits across exactly one boundary between two neighbouring runs. Given the lengths of those two runs, how many different values of k are possible?
Hint 3
Each pair of neighbouring runs with lengths a and b contributes min(a, b). Track only the previous run length and the current run length while scanning, and remember to add the final pair after the loop.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Split s into maximal runs of equal characters. For example, "00110011" has run lengths [2, 2, 2, 2].
A qualifying substring has the form x^k y^k with x != y. Its first half must be the end of one run, and its second half must be the start of the very next run. So every qualifying substring belongs to exactly one boundary between two adjacent runs.
For adjacent runs of lengths a and b, the valid values of k are 1, 2, ..., min(a, b). Each k gives exactly one substring around that boundary. So the boundary contributes min(a, b), and the answer is the sum of min over all adjacent run pairs.
Implementation
Keep only the previous run length and the current run length while scanning:
- If the character equals the previous one, extend the current run.
- Otherwise the current run has ended. Add
min(prev_len, cur_len), move the current length into the previous one, and start a new run of length 1. - After the loop, add
min(prev_len, cur_len)once more, because the last boundary is never followed by a character change.
At the first character, both lengths are 0, so the first addition is 0. An empty string or a single-run string gives 0.
Why it is correct
- Complete: every qualifying substring crosses exactly one run boundary, and it is counted for that boundary with its
k. - No duplicates: different
kvalues at one boundary give different substrings, and different boundaries give substrings at different positions. - No false positives: a string like
"0101"crosses several boundaries, so it never fits the two-block shape and is never counted.
Complexity
- Time: O(n), one pass over the string.
- Space: O(1).
Reference solution
def count_two_block_substrings(s: str) -> int:
prev_len = 0
cur_len = 0
total = 0
last_char = None
for ch in s:
if ch == last_char:
cur_len += 1
else:
# A run just finished; count the substrings across its left boundary.
total += min(prev_len, cur_len)
prev_len = cur_len
cur_len = 1
last_char = ch
# Count the boundary between the last two runs.
total += min(prev_len, cur_len)
return total