Problem Description
Given a binary string s and an integer numOps, you may flip up to numOps characters (i.e. change a '0' to a '1' or vice versa). The goal is to minimize the length of the longest contiguous substring where all characters are identical. Return that minimum possible length after performing at most numOps flips.
Key Insights
- We want to ensure that after at most numOps flips, no contiguous sequence of identical characters is longer than some target value L.
- Flipping a character in the middle of a long contiguous segment can break it into smaller segments.
- For each candidate L (the maximum allowed run length), we can simulate a greedy left-to-right process: whenever adding a character would exceed L, flip it (which resets the current run to 1) if flips remain.
- A binary search is used to efficiently determine the smallest L for which the simulation succeeds.
- This simulation takes O(n) time and the binary search over L takes O(log n) iterations, for an overall O(n log n) time per test case.
Space and Time Complexity
Time Complexity: O(n log n)
Space Complexity: O(1)
Solution
We use binary search on the answer L (the maximum allowed identical substring length after flips). For each candidate L, a greedy simulation scans through the string. We maintain the current streak (run) of identical characters. If adding the next character would push the run length over L, we flip that character (which resets the run to 1) and decrement our available operations. If at any point the number of flips required exceeds numOps, then candidate L is not achievable. Otherwise, if we can process the entire string, candidate L is valid and we search for a possibly smaller L.
The key data structure is a few integer counters and a character variable for the last processed (or flipped) character. The greedy approach is optimal because any time a run length would exceed L, we are forced to perform a flip so that the run stays at or below L.