Facebook PixelLongest Repeating Character Replacement — Coding Practice
Longest Repeating Character ReplacementMedium

Longest Repeating Character Replacement

Medium 9.6k53% acceptance
Hash TableStringSliding Window

You are given a string s of uppercase English letters and an integer k. You may pick at most k positions in the string and replace each chosen character with any uppercase letter.

Return the length of the longest substring that can be made up of a single repeated character after performing at most k replacements.

Example 1
Input: s = "ABAB", k = 2
Output: 4
Replace the two "A"s with "B" (or the two "B"s with "A") to get "BBBB" — length 4.
Example 2
Input: s = "AABABBA", k = 1
Output: 4
Replace the middle "A" in "ABBA" to get "BBBB", giving a run of length 4. One replacement is enough.
Constraints
  • 1 ≤ s.length ≤ 10⁵
  • `s` consists of uppercase English letters.
  • 0 ≤ k ≤ s.length
Asked atGoogleAmazonMetaMicrosoft
JavaScript
Loading editor…
Case 1
"ABAB", 2
expected: 4
Case 2
"AABABBA", 1
expected: 4
Case 3
"AAAA", 0
expected: 4
Case 4
"ABCDE", 1
expected: 2
Case 5
"A", 0
expected: 1
Case 6
"ABBB", 0
expected: 3
Case 7
"AAAB", 0
expected: 3