You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
Example 1:
Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.
Constraints:
1 <= s.length <= 105s consists of only uppercase English letters.0 <= k <= s.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the longest repeating character replacement involves checking every possible piece of the text and seeing if we can make it all the same letter by swapping a limited number of characters. We simply test every single option exhaustively from start to finish to see which one gives us the longest stretch.
Here's how the algorithm would work step-by-step:
def character_replacement(input_string, max_replacements):
maximum_length = 0
string_length = len(input_string)
# Iterate through every possible starting point to evaluate all substrings
for start_index in range(string_length):
for end_index in range(start_index, string_length):
current_window = input_string[start_index:end_index + 1]
character_counts = {}
highest_frequency = 0
for character in current_window:
character_counts[character] = character_counts.get(character, 0) + 1
if character_counts[character] > highest_frequency:
highest_frequency = character_counts[character]
window_length = end_index - start_index + 1
characters_to_change = window_length - highest_frequency
# Ensure the required replacements do not exceed the allowed limit
if characters_to_change <= max_replacements:
if window_length > maximum_length:
maximum_length = window_length
return maximum_lengthInstead of checking every possible section of letters, we use a sliding window approach that expands as long as the rule is followed. When the rule is broken, the window simply slides forward instead of shrinking, ensuring we only ever track the longest valid section found.
Here's how the algorithm would work step-by-step:
def character_replacement(input_string, allowed_replacements):
character_counts = {}
left_pointer = 0
max_frequency = 0
for right_pointer in range(len(input_string)):
current_character = input_string[right_pointer]
character_counts[current_character] = character_counts.get(current_character, 0) + 1
max_frequency = max(max_frequency, character_counts[current_character])
# We only slide the window instead of shrinking it because we only care about beating our longest valid length.
if (right_pointer - left_pointer + 1) - max_frequency > allowed_replacements:
left_character = input_string[left_pointer]
character_counts[left_character] -= 1
left_pointer += 1
# The total window size remains at its maximum achieved length because we never shrink the sliding frame.
return right_pointer - left_pointer + 1| Case | How to Handle |
|---|---|
| Empty string input where s is empty | The sliding window loop will not execute and naturally return zero as the longest length |
| String length is smaller than or equal to k | The entire string can be converted to a single character so the length of the string is returned |
| String contains all identical characters | The window expands to the full length of the string since no replacements are needed |
| K is equal to zero | The algorithm finds the length of the longest existing contiguous block of identical characters without making any replacements |
| Maximum possible constraint values for string length up to 10 to the 5th power | The two-pointer sliding window approach scales linearly with O of n time complexity to pass efficiently |
| Alternating characters with a small k value such as ababab and k equals one | The window correctly slides and maintains a maximum valid length of three by replacing a single alternating character |
| Frequency count optimization where max frequency is not recomputed when the window shrinks | Our solution relies on a historical max frequency which is sufficient because a smaller valid window size cannot be beaten anyway |