Skip to content
AI360Xpert
Beta
Difficulty: MediumSliding Window

Longest Repeating Character Replacement

Problem in Plain English

Given a string of uppercase letters and an integer k, you may change at most k characters to any other uppercase letter. Return the length of the longest substring that can be turned into a run of one repeated letter.

Problem Statement

Given a non-empty string s of uppercase English letters and an integer k, you may replace at most k characters with other uppercase English letters. Return the maximum length of a contiguous substring that can contain just one repeated letter after those changes. Return a length, not the changed string, and do not actually modify s.

For a candidate window of length L with most frequent letter count F, keeping that letter and replacing all others costs exactly L - F changes. The window is genuinely feasible when L - F <= k.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only uppercase English letters.
  • 0 <= k <= s.length

Examples

Example 1

Input
s = "ABAB", k = 2
Output
4

Replace the two A letters with B letters (or vice versa) to get "BBBB", a run of length 4.

Example 2

Input
s = "AABABBA", k = 1
Output
4

Replace the middle A with B to get "AABBBBA", which contains the run "BBBB" of length 4.

Example 3

Input
s = "ABAB", k = 0
Output
1

No replacements are allowed, and adjacent letters differ, so only a single-letter substring is uniform.

Example 4

Input
s = "AAAA", k = 0
Output
4

The full string is already one repeated letter and costs zero replacements.

Example 5

Input
s = "ABCDE", k = 5
Output
5

Keeping any one letter requires replacing four positions, within the allowed budget. The answer cannot exceed the original length.

Intuition

Keep the letter already occurring most often in a candidate substring, since every other letter costs one replacement. In ABAB, A and B each occur twice: keeping either one changes two of the four positions, so k = 2 allows the full length 4. Extending a window may create a better answer; removing from its left lets the search continue without restarting. The optimized algorithm remembers a historical frequency bound to search for a length, so its current window need not always be genuinely feasible.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
For a chosen substring, which existing letter should stay unchanged to minimize the number of replacements?
Hint 2
If length is L and its most frequent letter occurs F times, the exact replacement cost is L - F. Counts over the fixed 26-letter alphabet make this inexpensive.
Hint 3
A linear length search can retain the largest frequency seen instead of recomputing it after removals. Treat that value as historical, not as proof that every currently retained window is feasible.

Edge Cases

  • One character returns 1 for either permitted k value. Empty strings are outside this problem's public constraints.
  • With k = 0, the answer is the longest existing contiguous run; ABAB returns 1.
  • Uniform input such as AAAA returns 4 even with zero budget. It is also a quadratic worst case for the baseline.
  • A budget equal to n allows the entire string, although at most n - 1 replacements are actually needed.
  • AABABBA with k = 1 returns 4 even though its final retained ABBA window requires two changes. The recorded length has an earlier feasible witness.

Common Mistakes and Interview Tips

  • Treating historical maxFreq as the exact maximum after a removal can falsely label an infeasible current window valid. It is a bound used to search for a length.
  • Returning the final substring from this length-only optimization may return an infeasible example; the contract asks only for a length.
  • Decrementing maxFreq to the outgoing character's new count is not a correct exact-maximum update. Recomputing all 26 counters is a valid alternative, but requires the exact-window shrink rule and a different proof.
  • Using a strict comparison that rejects exactly k replacements drops valid windows. The cost is permitted when it is <= k.
  • Interview tip: derive the exact length-minus-frequency rule before discussing historical counts. Explain why an old record may be retained without a feasible current window and why a new record still has a witness.

Key Takeaway

A budgeted window can use a historical frequency bound when the requested output is a maximum length and record growth has a feasible witness. Distinguish this optimization from maintaining an exactly valid current substring.