← All Patterns

Pattern Recognition Drill

#103

Medium Sliding Window

The Problem

Given string s and integer k, find the longest substring where you can replace at most k characters to make all characters the same.

What approach would you use?

Think about it before scrolling down.

Longest Repeating Character Replacement

Key Signals

Sliding Window

Window is valid when (window_size - max_freq_char) <= k. Expand right, shrink left when invalid. O(n).

Alt: Binary Search

Binary search on answer length, check each length feasibility. O(n log n).

Common Trap

You don't need to update max_freq when shrinking — the window only grows when a better max is found.

← #102 #104 →