‹ DS&A interview · Socratic
DSA · Sliding window · #13

Longest Repeating Character Replacement

Module 16 · difficulty 3/5·30:00starts on first keystroke

You are given a string `s` and an integer `k`. You may choose any `k` characters in the string and replace each with any other uppercase English letter. Return the length of the longest substring containing the same letter you can obtain after performing at most `k` replacements.

Examples
  • s = "ABAB", k = 2 4replace the two As with B (or vice versa)
  • s = "AABABBA", k = 1 4replace the middle A → "AABBBBA" has "BBBB"
  • s = "AAAA", k = 0 4
Constraints
  • · 1 <= s.length <= 10^5
  • · s consists of uppercase English letters.
  • · 0 <= k <= s.length
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.