Trains the technique from
LeetCode 696Count Binary SubstringsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A sorting line marks every item that passes the head with one of two stamps, recorded as the character '0' or '1'. One shift comes back as the string stamps, holding the marks in the order the items went by.
A paired stretch is a run of consecutive positions in the record that is made of two solid blocks: some number of one stamp, immediately followed by the same number of the other stamp. Nothing inside either block may break it.
Return how many paired stretches the record contains. A stretch is identified by where it begins and where it ends, so two stretches that read alike but sit at different places in the record both count, and stretches are allowed to overlap.
Example 1
The paired stretches are positions 2 to 3 reading 01, positions 1 to 4 reading 0011, positions 5 to 6 reading 10, and positions 4 to 7 reading 1100.
Example 2
Only positions 3 to 4, reading 10, split into two solid blocks of equal length.
Example 3
The record carries one stamp throughout, so no stretch of it holds a block of each stamp.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_paired_blocks(stamps: str) -> int:public int countPairedBlocks(String stamps)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.