Use a sliding window and keep track of the most frequent character count in the window.
Expand the window to the right, updating counts and max_freq. If the window needs more than k replacements (window_len - max_freq > k), shrink from the left.
A window is valid if you can replace all non-majority characters within k. Tracking max_freq lets you check validity in O(1).
Use a sliding window and keep track of the most frequent character count in the window.
Expand the window to the right, updating counts and max_freq. If the window needs more than k replacements (window_len - max_freq > k), shrink from the left.
A window is valid if you can replace all non-majority characters within k. Tracking max_freq lets you check validity in O(1).