Trains the technique from
LeetCode 3399Smallest Substring With Identical Characters IIThis 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 tape reads s, a string of '0' and '1'. Up to numOps characters may be flipped to the other one, chosen freely and independently.
A run is a stretch of equal neighbouring characters. Make the tape's longest run as short as you can, and return that length.
Example 1
Four flips are enough to leave no run longer than one, since the tape needs four characters changed to alternate throughout.
Example 2
One flip splits the run of ten into pieces, and the best it can do is leave a longest run of five.
Example 3
With no flips allowed the tape keeps its single run of ten.
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 min_length(s: str, numOps: int) -> int:public int minLength(String s, int numOps)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.