DSA Tracker

Problem

Pattern this teaches:Sliding Window

Medium

Longest Repeating Character Replacement

A medium Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Strings
Sheets
2
Core for
14 roles

The problem

You may change up to k characters of the string to any letter you like. After those changes, how long can a stretch of one repeated letter be? Return that length.

Example 1

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

Example 2

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

Example 3

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

Constraints

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

How to think about it

Updated 2026-09-29

The best character to turn everything else into is always whichever character already appears most often inside your current window. Every other character in that range has to be overwritten with one of your k changes. When the number of non-majority characters surpasses k, the window has become invalid and its left boundary must slide forward.

Approaches, worst first

  1. Test all substrings

    time O(n^2) · space O(1)

    Iterate over every pair of start and end indices, count the frequency of each letter, and check whether window length minus the maximum letter count is at most k. It recomputes frequency counts from scratch for overlapping windows.

  2. Sliding window with frequency mapWrite this one

    time O(n) · space O(1)

    Expand the right pointer while tracking letter counts and the highest frequency seen in any valid window so far. When window length minus max frequency exceeds k, shift the left pointer right by one. The window only ever grows or shifts, maintaining the record.

Where people lose marks · 3
  • Shrinking the window does not require decreasing max frequency. A smaller window cannot beat the best valid length already found, so max frequency only needs to update when it grows.
  • Assuming k can be larger than string length; here k <= s.length, but when k equals or exceeds s.length minus one, the whole string can become identical and must return s.length.
  • Off-by-one on the window size formula: the length between indices left and right is right - left + 1.

The theory behind it

Strings — the ground this problem stands on. All Strings problems

What Strings is

A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.

When to reach for it

Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.

How the pattern works

Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.

More Strings problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .