Longest Repeating Character Replacement

MediumSliding Window

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

You may perform at most k operations. Each operation changes one character of s into any other uppercase letter.

Return the length of the longest substring that consists of a single repeated letter after your operations.

Constraints

  • 1 <= s.length <= 10^5
  • s contains uppercase English letters only.
  • 0 <= k <= s.length

Examples

Example 1

Input: s = "ABBBCB", k = 1
Output: 5

Replace the C with a B: "BBBBB" (indices 1 to 5) has length 5.

Example 2

Input: s = "AAAA", k = 0
Output: 4

The whole string is already one letter.

Example 3

Input: s = "ABCD", k = 0
Output: 1

With no replacements allowed, the best is a single letter.

JavaScript you'll use: Arrays: create, read, update Strings and characters

/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
function characterReplacement(s, k) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.