Longest single-colour run after repainting k tiles

Sliding window Mid-level Python 3.13+ Reviewed By Best Latest Apps

Pick the fix

When should the window shrink?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A When its length minus its top colour count is more than k

Right: that many tiles would need repainting, more than allowed.

The test counts the tiles that are not the majority colour, since those are the ones you would repaint.

Why not the others?
  • B When it contains more than one colour. Mixed colours are fine as long as repaints cover the minority tiles.
  • C When its length is more than k. Long windows are the goal; only the number of minority tiles matters.

30-second answer

A window is fixable if its length minus the count of its most common colour is at most k. Grow the window, update counts and the best count seen, and shrink by one when the window is not fixable. The best count never needs to decrease for the answer to stay right, so each step is O(1): O(n) total, with O(c) space for the counts of c colours.

Practise saying it · 60 s. No recording, just a timer

Explain the repaint-budget window.

Say your answer out loud, then check which of these points you covered:

  • Length minus top count
  • Shrink when over k
  • O(n)

Deep dive

Not lowering top when the window shrinks is safe: the answer only grows when a window with a higher top count appears.

Recomputing max(counts) each step is O(26) and also fine; it is easier to explain.

The window never shrinks by more than one per step, so its size is the best answer so far.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def longest_after_repaints(row: str, k: int) -> int:
    counts: dict[str, int] = {}
    left = top = best = 0
    for right, colour in enumerate(row):
        counts[colour] = counts.get(colour, 0) + 1
        top = max(top, counts[colour])
        if right - left + 1 - top > k:
            counts[row[left]] -= 1
            left += 1
        best = max(best, right - left + 1)
    return best

Common mistakes

  • Trying every colour as the target separately and doing it badly in O(26·n²).
  • Shrinking in a while loop and recomputing the top count every time, then getting confused about correctness.

Senior take & follow-ups

If you use the 'never decrease top' trick, explain why it is safe; otherwise recompute and say it is O(26n).

Follow-up questions

Think of your answer first, then open the hint.

  1. What if repaints have different costs per tile?

    Show hint

    Then it is a budget window with costs instead of counts.

  2. How do you return which colour wins?

    Show hint

    Track the colour that set top when best improves.

What each operation costs: Arrays · Deque

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.