Longest Repeating Character Replacement

Medium

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.





Thanks for feedback.

Share Your Thoughts




Explore more in Fundesk DSA Curated Problems List
Count triplets with sum less than given value
Find a pair with given sum in a rotated sorted array
Find minimum no of coins required for a given amount
Count Palindromic Substrings
Count the number of palindromic subsequences
Search an array where adjacent differ by at most k
Browse all Fundesk DSA Curated Problems List articles →