MediumHash TableStringSliding Window

Longest Repeating Character Replacement

LeetCode
1 approach, code in all languages

Given a string s of uppercase English letters and an integer k, you may choose at most k positions and overwrite each with any uppercase letter of your choice. Return the length of the longest substring that can be made into a single repeated letter after those changes.

You do not have to use all k changes.

Example 1

Input: s = "ABAB", k = 2

Output: 4

Change the two B's into A's (or the two A's into B's) to make the whole string uniform.

Example 2

Input: s = "AABABBA", k = 1

Output: 4

One change turns a window like "AABA" into "AAAA", giving a run of length four.

Constraints

  • 1 <= s.length <= 10^5
  • 0 <= k <= s.length
  • s consists of uppercase English letters only.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now