Skip to main content
mediumBlind 75

Longest Repeating Character Replacement

Given a string s and an integer k You can replace at most k characters to make a substring of repeating characters Return the length of the longest such substring Example: s = "ABAB", k = 2 => 4 (replace both A's with B's) Example: s = "AABABBA", k = 1 => 4 (replace one A to get "BBBB")

Given a non-empty string s of uppercase English letters and an integer k >= 0, you may change at most k characters of s into any other uppercase letter. Return the length of the longest substring that can be made to consist of one repeated letter. For example, 'AABABBA' with k = 1 gives 4.

Examples

You write

character_replacement(s: str, k: int)

  1. Example 1

    Input
    s = "ABAB", k = 2
    returns
    4
  2. Example 2

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

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Find Longest Substring with K Replacementsmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.