Compile Ready
Module 3 · Variable Window

Longest Repeating Character Replacement

MediumProblem 5 of 17 9 min read ~20 min to solve LeetCode
Sliding WindowTwo PointersFrequency CountingStringVariable Window
Asked atAmazonGoogleMicrosoftMetaUber

Problem Statement

Given a string s containing only uppercase English letters and an integer k, return the length of the longest substring that can be transformed into a substring with the same repeated character by replacing at most k characters.

Input

A string s of uppercase letters and an integer k, the maximum number of replacements allowed.

Output

An integer: the maximum length of a contiguous substring that can become all one character using at most k replacements.

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
Explanation: Replace both **A** characters or both **B** characters to make the entire string equal.

Example 2

Input:
s = AABABBA, k = 1
Output: 4
Explanation: The substring **AABA** can become **AAAA** by replacing one **B**, and no length 5 window is valid.

Example 3

Input:
s = AAAA, k = 0
Output: 4
Explanation: The full string already consists of one repeated character.

Learning Objectives

  • Translate replacement budget into the validity test **window length - max frequency <= k**.
  • Maintain frequency counts while expanding and shrinking a variable window.
  • Understand why the tracked maximum frequency does not need to decrease during shrinking.
  • Use the longest-valid template without checking every target character separately.

Intuition

Pattern Identification

This is a longest-valid variable-window problem. For any fixed window, the best target character is the character that already appears most often. Every other character must be replaced, so the window is valid exactly when window length - max frequency in window <= k.

The expand and shrink invariant is budget feasibility. Expand right to try a larger answer. If the number of needed replacements exceeds k, shrink left until the window size is no longer beyond what the current best frequency can support. The subtle greedy insight is that max frequency can be kept as the largest value ever seen while expanding; an overestimated value may delay shrinking, but it never causes the final best length to exceed a length that was supported when that maximum frequency was achieved.

Common mistakes

  • ×Counting replacements against the first character in the window instead of the most frequent character.
  • ×Recomputing the maximum frequency from scratch on every shrink for no benefit.
  • ×Shrinking when **window length - max frequency == k** even though the window is still valid.
  • ×Trying all 26 target letters separately when one frequency table is enough.

Algorithm Explanation

Window setup

Keep left, right, a frequency table for the 26 uppercase letters, and maxFrequency, the largest count of any letter observed in the current expansion history. A window of length L is valid when L - maxFrequency <= k.

Window visualization

For s = AABABBA and k = 1, the window grows through AABA. Its length is 4 and the highest frequency is 3 for A, so only one replacement is needed and best = 4. When right reaches the next B, the window AABAB has length 5 and max frequency 3, so it would need 2 replacements. Shrink left once to keep the search focused on windows that can match the current budget.

Algorithm

  1. Initialise left = 0, best = 0, maxFrequency = 0, and a 26-entry frequency table.
  2. For each right, add s[right] to the table and update maxFrequency.
  3. If right - left + 1 - maxFrequency > k, remove s[left] and increment left.
  4. Update best with the current window length.
  5. Return best after the scan.

Solutions

Solution: Frequency window with replacement budget

The frequency table tells us the cheapest character to make the whole window equal to: keep the majority letter and replace the rest. The window only shrinks when the number of non-majority characters exceeds k.

Step-by-step

  1. Count letters as the right boundary expands.
  2. Keep maxFrequency as the largest count reached by any letter during expansion.
  3. When the window would need more than k replacements, remove the leftmost letter and move left forward.
  4. Record the largest window length seen after the budget check.
  5. Return that length.
Time

O(n)

Space

O(1)

The scan is linear and the frequency table always has 26 entries.

Java implementation

Loading…

Dry Run

Sample input

s = AABABBA, k = 1. Track the window length, the best majority count, replacements needed, and the best answer.

stepright charleftwindowmaxFrequencyneeded replacementsbest
1A at 00A101
2A at 10AA202
3B at 20AAB213
4A at 30AABA314
5B at 41ABAB31 after shrinking4
6B at 52BABB31 after shrinking4
7A at 63ABBA31 after shrinking4

The longest valid window length is 4. The table keeps maxFrequency = 3, which is enough to preserve the correct best length.

Interview Tips

Lead with the formula window length - max frequency because it explains the whole problem. Be ready to justify stale maxFrequency: it may make the current window look better than it is, but the answer length was achievable when that frequency was originally present, and the window only grows one step at a time.

Likely follow-ups

  • What if the alphabet were much larger than 26 characters?
  • How would you return the substring bounds as well as the length?
  • How would the solution change if different characters had different replacement costs?
  • How would you solve the binary version where you may flip at most **k** zeroes?

Similar Problems

Key Takeaways

  • The best replacement target inside a window is its most frequent character.
  • A window is valid when non-majority characters fit inside the replacement budget.
  • For this longest-window problem, **maxFrequency** does not need to decrease while shrinking.
  • Shrink only when the budget is exceeded, not when it is exactly used.
Reusable template: For longest replacement windows, track the majority frequency, treat the rest as required edits, and shrink only while required edits exceed the allowed budget.