Repaint the fence
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A fence is a row of boards, each painted a colour. fence describes it, one capital letter per board: "ABAB" is four boards, alternating colours A and B.
You can repaint at most k boards, each to any colour. Return the length of the longest stretch of boards in a row that can end up all the same colour.
Examples
Example 1
- Input
- fence = "XYXY", k = 2
- Output
- 4
Repaint both X's to Y (or both Y's to X): all 4 boards match.
Example 2
- Input
- fence = "CABCCAC", k = 1
- Output
- 4
Boards 3 to 6 are "CCAC". Repaint the A and they're all C.
Example 3
- Input
- fence = "XYZ", k = 0
- Output
- 1
No repaints allowed, and no two neighbours match.
Rules
fencehas between 0 and 100,000 boards, each a capital letter A to Z.kis between 0 and the number of boards.
Words on this page, in plain English (1)Open if any word is new to you
- Length
- How many items a list (or letters a string) has. The last position is always
length - 1.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We're looking for the longest stretch of boards in a row that could become one colour using at most k repaints. We don't repaint anything for real; we just measure how long that stretch could be.
2. Solve an example by hand
Look at one stretch, say CCAC. To make it one colour cheaply, keep the colour there's most of (C, three boards) and repaint the rest (one A). So:
repaints needed = length of the stretch − boards of its most common colour
For CCAC that's 4 − 3 = 1. With k = 1, it works.
3. The slow way
Check every stretch with that formula: O(n²) stretches. For 100,000 boards that's 5 billion.
4. Spot the pattern
The longest stretch of neighbouring items that follows a rule ("needs at most k repaints"): a growing and shrinking window. To know the most common colour inside the window, keep a Map of colour counts, updated as boards come and go.
5. The window idea
For each new board at right: add it to the counts. If the window now needs more than k repaints, drop boards from the left until it doesn't. Then the window works; keep its length if it's the best.
A window that needs at most k repaints
fence
- k =
- 1
- repaints needed =
- 0
- best =
- 0
counts (Map)
| colour | boards |
|---|---|
| "C" | 1 |
function repaintStretch(fence, k) { const counts = new Map(); // colour → boards of that colour in the window let left = 0; let best = 0; for (let right = 0; right < fence.length; right++) { counts.set(fence[right], (counts.get(fence[right]) ?? 0) + 1); // Keep the most common colour, repaint the rest. while (right - left + 1 - Math.max(...counts.values()) > k) { counts.set(fence[left], counts.get(fence[left]) - 1); left++; } best = Math.max(best, right - left + 1); } return best;}Add board 0 (C). The window "C" needs 0 repaints.
6. The code, line by line
const counts = new Map(); // colour → boards of that colour in the window let left = 0; let best = 0;countssays how many boards of each colour are in the window.for (let right = 0; right < fence.length; right++) { counts.set(fence[right], (counts.get(fence[right]) ?? 0) + 1);A new board joins the window: count its colour.
// Keep the most common colour, repaint the rest. while (right - left + 1 - Math.max(...counts.values()) > k) { counts.set(fence[left], counts.get(fence[left]) - 1); left++; }Repaints needed is the window's length minus its most common colour's count.
Math.max(...counts.values())finds that count. While it's more thank, drop the left board from the counts and movelefton.best = Math.max(best, right - left + 1);The window works: keep its length if it's the longest yet.
7. How fast is it?
| Every stretch | Sliding window | |
|---|---|---|
| Time | O(n²) | O(26 × n) = O(n): finding the biggest count looks at up to 26 colours |
| Extra memory | O(26) | O(26) = O(1) |
There's a well-known trick that avoids recomputing the biggest count, by keeping the highest count ever seen. It's faster, but much harder to convince yourself it's correct. Clear code that's still O(n) is a good trade.
Common mistakes
- Counting the repaints as "boards that differ from the first board". Keep the most common colour, not the first one.
- Forgetting to lower the count when
leftmoves. The Map must describe only the boards inside the window.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Repaint the fence
Read the 14 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function repaintStretch(fence, k) { const counts = new Map(); // colour → boards of that colour in the window let left = 0; let best = 0; for (let right = 0; right < fence.length; right++) { counts.set(fence[right], (counts.get(fence[right]) ?? 0) + 1); // Keep the most common colour, repaint the rest. while (right - left + 1 - Math.max(...counts.values()) > k) { counts.set(fence[left], counts.get(fence[left]) - 1); left++; } best = Math.max(best, right - left + 1); } return best;}