Coding interview problems โ€บ Sliding Window

๐ŸชŸ Sliding Window Medium

Longest Repeating Character Replacement

Longest Repeating Character Replacement explained: a window is valid while its length minus the top letter count is at most k. O(n) window, tested Python.

Also searched as: character replacement sliding window ยท longest substring with same letters after k replacements

๐ŸชŸ

Try it live Free

Sliding Window

Watch the window slide by adding the element that enters and removing the one that leaves. Here what you track inside the window is a letter count, and the window is valid while length minus top count is at most k.

Open the playground โ†’

The problem

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

Inputs = "ABAB", k = 2
Output4

Change both As or both Bs.

Inputs = "AABABBA", k = 1
Output4

"AABA" becomes "AAAA".

The key insight

A window can be turned into one letter if the characters that are not its most frequent letter number at most k, that is window_length - max_count <= k. Grow the window on the right; when it becomes invalid, slide the left edge forward by one so the window never shrinks below the best length found.

Brute force first: Testing every substring and its letter counts is O(nยฒ) or more.

Step-by-step approach

  1. Keep letter counts for the window, left = 0, and max_count, the highest count seen in a window.
  2. For each right, increment the count of s[right] and update max_count.
  3. If (right - left + 1) - max_count > k, decrement the count of s[left] and move left forward one step.
  4. Track the best window length right - left + 1.
  5. Return the best length.

Python solution

PYTHON 3tested
def character_replacement(s, k):
    count = {}
    left = max_count = best = 0
    for right, ch in enumerate(s):
        count[ch] = count.get(ch, 0) + 1
        max_count = max(max_count, count[ch])
        if (right - left + 1) - max_count > k:   # too many to replace
            count[s[left]] -= 1
            left += 1
        best = max(best, right - left + 1)
    return best

Complexity

TimeO(n)
SpaceO(1)

Each character enters and leaves the window at most once. The count map holds at most 26 letters.

Common mistakes

Frequently asked questions

What is the time and space complexity of Longest Repeating Character Replacement?

O(n) time and O(1) space, since the counts cover at most 26 uppercase letters.

Why is it fine that max_count is never decreased?

The answer only improves when a window has a larger max_count than any before. A stale max_count can keep an invalid window the same size, but the recorded best length was achieved by a real valid window earlier.

What if the string has any characters, not just uppercase?

Use a dictionary for counts instead of a 26-slot array. The logic is unchanged and space becomes O(k) for k distinct characters.

Preparing for placements?

Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.

All coding interview problems More Sliding Window Sliding Window lab Placement prep guides

Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.