Smallest Substring With Identical Characters II
Real-world version: Shortest Longest Run After FlipsThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- s = "1100011100", numOps = 4
- Output
- 1
Four flips are enough to leave no run longer than one, since the tape needs four characters changed to alternate throughout.
Example 2
- Input
- s = "0000000000", numOps = 1
- Output
- 5
One flip splits the run of ten into pieces, and the best it can do is leave a longest run of five.
Example 3
- Input
- s = "0000000000", numOps = 0
- Output
- 10
With no flips allowed the tape keeps its single run of ten.
Constraints
- 1 <= s.length <= 10^5
- s[i] is either '0' or '1'
- 0 <= numOps <= s.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def min_length(s: str, numOps: int) -> int:public int minLength(String s, int numOps)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Search Suggestions SystemReal-world version: Prefix Hints for the Parts CatalogueMedium
- Find Beautiful Indices in the Given Array IReal-world version: Nearby Marker PositionsMedium
- Shortest Way to Form StringReal-world version: Fewest Passes Of The Letter StencilMedium
- Get Equal Substrings Within BudgetReal-world version: Longest Stretch Retinted Within BudgetMedium