ExamAdda Logo

Longest Repeating Character Replacement

Easy

Given a string s consisting of uppercase English letters and an integer k, you can replace at most k characters in the string with any uppercase English letter.

Return the length of the longest substring containing the same character after performing at most k replacements.

Example 1

Input

ABAB
2

Output

4

Explanation

Replace both A characters with B, or both B characters with A, to make the entire string identical.

Example 2

Input

AABABBA
1

Output

4

Explanation

By replacing one character, a longest valid substring such as AABA can be made entirely of A characters.

Constraints

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

Hints:

Hint 1

Use a sliding window to maintain a substring that can be made identical using at most k replacements.

Hint 2

Track the most frequent character in the current window. The number of replacements required is the window length minus that character's frequency.

Author & Technical Reviewer

Chatla Jithin Sri Surya

Written by: Chatla Jithin Sri Surya

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

Published

Oct 27, 2026

Last updated

Oct 27, 2026

Last technically reviewed

Oct 27, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output