← DiffPush

Longest Repeating Character Replacement

Standard Bar Sliding Window · Medium Problems O(n) · O(26)

The Mural Touch-Up Painter

A restorer inspects a long mural strip of colored tiles, each already painted one uppercase-coded color, and carries k cans of touch-up paint — each can repaints one tile into any color. The studio asks for the longest solid single-color band the restorer can produce. The painter evaluates a frame by asking one question: can the minority tiles inside it all be repainted within budget?

Input: A string s of uppercase English letters and an integer k, the repaint budget.

Output: Return the length of the longest single-letter substring achievable with at most k replacements.

Constraints

Examples

Example 1

Input: {"s":"ABAB","k":2}
Output: 4
Two repaints turn either letter into the other across the whole strip.

Example 2

Input: {"s":"AABABBA","k":1}
Output: 4
The best band is "ABBA"-shaped: one repaint makes it four identical letters.

Solve this in your browser →

Also on LeetCode ↗