557. Longest Substring with At Most K Replacements

HardBinary SearchSliding WindowString

Given a string s of uppercase letters and an integer k, you may replace at most k characters with any uppercase letters. Return the length of the longest substring containing a single repeated letter achievable after the replacements. The input is JSON {s, k}.

Input: JSON {s, k}.

Output: Integer — the longest achievable run length.

Examples

Example 1
Input: {"s":"AABABBA","k":1}
Output: 4
Explanation: Replace one character to get a run of 4.
Example 2
Input: {"s":"ABAB","k":2}
Output: 4
Explanation: Two replacements unify the string.
Example 3
Input: {"s":"AAAA","k":0}
Output: 4
Explanation: Already uniform.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →