Problem · String

Longest Repeating Character Replacement

Learn this problem
MediumByteDance logoByteDanceFULLTIMEPHONE SCREEN

Problem statement

You are given a string s containing only uppercase English letters and an integer k. You may replace at most k characters in s with any other uppercase English letter.

Return the length of the longest substring that can contain only one repeated letter after at most k replacements.

Function

characterReplacement(s: String, k: int) → int

Examples

Example 1

s = "ABAB"k = 2return = 4

Replace both occurrences of A with B, or both occurrences of B with A. The entire string then contains one repeated letter.

Example 2

s = "AABABBA"k = 1return = 4

The substring AABA can become AAAA with one replacement, so a length of 4 is possible.

Constraints

  • 1 <= s.length <= 100000
  • s contains only uppercase English letters.
  • 0 <= k <= s.length

More ByteDance problems

drafts saved locally
public int characterReplacement(String s, int k) {
    // Write your code here.
}
s"ABAB"
k2
expected4
checking account