2Brute2Best
🔥0

Longest Repeating Character Replacement

Medium·LeetCode #424 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a string s of uppercase letters and a number k.

You may pick at most k positions in the string and change the letter at each one to any other uppercase letter.

After your changes, return the length of the longest substring in which every letter is the same.

Input

A string s of uppercase English letters and an integer k.

Output

One integer: the longest run of one repeated letter you can make with at most k changes.

Example 1
Input: s = "ABAB", k = 2
Output: 4

Change both A's to B (or both B's to A) and the whole string is one letter.

Example 2
Input: s = "AABABBA", k = 1
Output: 4

Change the A at index 3 to B to get "AABBBBA". The block "BBBB" has length 4. Other choices also give 4.

Constraints
  • • 1 ≤ s.length ≤ 10^5
  • • s contains only uppercase English letters
  • • 0 ≤ k ≤ s.length
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“longest substring”
→ Contiguous range, maximise length → variable window.
“change at most k characters”
→ A budget of k "bad" letters inside the window.
“all letters the same”
→ The "good" letters are the most common letter; the bad ones are everything else → need counts and the max count.
03

Approach Complexity Ladder

Brute Force — every start, grow while fixableO(N²)O(1)

For each start, grow right while length − maxFreq ≤ k

Better — sliding window, recount the max over 26 lettersO(26 · N)O(1)

Grow right; while length − (max of 26 counts) > k, shrink left

Optimal — sliding window with a never-decreasing maxFreqO(N)O(1)

Keep the biggest count ever seen; the window slides or grows, never shrinks

Brute Force Intuition

Try every start. Grow the end one letter at a time, keeping counts and the most common count maxFreq of the substring. The substring needs length − maxFreq changes. While that is ≤ k, it is a candidate. Once it is > k, stop for this start.

Recognition rule for this sub-pattern:

Validity depends on the counts of things inside the window (distinct types, most frequent char, required letters). Keep a count map that updates on every add/remove.
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.