Skip to main content
Back to problems
Leetcode
Medium
Strings
Hash Maps
Sliding Window
Google
Meta
Longest Repeating Character Replacement

Find the length of the longest substring that can be turned into a string of identical characters by replacing at most k characters.

Acceptance 80%
Problem Statement

Problem

You are given a string s consisting of uppercase English letters and an integer k.

In one move, you may choose any character in a substring and replace it with any other uppercase English letter.

Return the length of the longest substring that can be converted into a string where every character is the same after performing at most k replacements.

Goal

Find the maximum possible length of a contiguous substring that can be made uniform using no more than k changes.

Input Format

  • A string s of uppercase English letters.
  • An integer k representing the maximum number of replacements allowed.

Output Format

  • Return a single integer: the maximum length of a valid substring.

Constraints

  • 1 <= s.length <= $10^{5}$
  • 0 <= k <= s.length
  • s contains only uppercase English letters A-Z.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "ABAB", k = 2

Output

4

Explanation

Replace both A characters with B, or both B characters with A, so the whole string becomes uniform.

Example 2

Input

s = "AABABBA", k = 1

Output

4

Explanation

One optimal choice is the substring AABA. Replace one B with A to make it AAAA.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.