Longest Repeating Character Replacement

Given a string s and an integer k. Choose any character of the string and change it to any other uppercase English character. You can do this at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

 

Example

Input: s = "ABAB", k = 2 Output: 4

Explanation: Replace the two 'A's with two 'B's or two 'B' with two 'A'. The resultant string will be “AAAA” or “BBBB”. So the length longest substring with same letters will be 4 in any case.

 

Input: s = "AABABBA", k = 1 Output: 4

Explanation: Replace 'A' in the middle of string with 'B' to get "AABBBBA".

The substring "BBBB" has the longest repeating letters, which is 4.


Share Your Thoughts

Explore more in Fundesk DSA Curated Problems List

Counting Sort
Remove duplicates from an unsorted singly linked list
Count the number of palindromic subsequences
Count triplets with sum less than given value
Permute Two Arrays such that Sum of Every Pair is Greater or Equal to K
Search an array where adjacent differ by at most k
Browse all Fundesk DSA Curated Problems List articles

Stay Ahead

Only insights that save you time or money. No fluff, ever.

Stay Ahead

Only insights that save you time or money. No fluff.