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 <= 105
s
consists of only uppercase English letters.0 <= k <= s.length
When 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:
We want to find the longest section of a string that has at most a certain number of different characters if we're allowed to change some characters. The brute force way is to check every possible section. We try every section length and starting position until we find the longest one that works.
Here's how the algorithm would work step-by-step:
def longest_repeating_character_replacement_brute_force(input_string, allowed_changes):
longest_section_length = 0
string_length = len(input_string)
for section_length in range(1, string_length + 1):
for start_index in range(string_length - section_length + 1):
end_index = start_index + section_length
substring = input_string[start_index:end_index]
# Determine the character that appears most in the substring
most_frequent_character_count = 0
for char in set(substring):
most_frequent_character_count = max(
most_frequent_character_count,
substring.count(char)
)
# Checks if changes are within the allowed limit
changes_needed = section_length - most_frequent_character_count
if changes_needed <= allowed_changes:
# Update longest_section_length
longest_section_length = max(longest_section_length, section_length)
return longest_section_length
The key to solving this efficiently is to avoid redundant calculations. We'll use a sliding window to keep track of a range of characters and dynamically adjust its size based on the allowed replacements, ensuring we find the longest valid substring.
Here's how the algorithm would work step-by-step:
def character_replacement(
input_string: str, allowed_replacements: int
) -> int:
window_start = 0
max_length = 0
most_frequent_character_count = 0
character_frequency = {}
for window_end in range(len(input_string)):
right_character = input_string[window_end]
if right_character not in character_frequency:
character_frequency[right_character] = 0
character_frequency[right_character] += 1
most_frequent_character_count = max(
most_frequent_character_count,
character_frequency[right_character],
)
# Shrink the window if replacements exceed the limit.
if (
window_end - window_start + 1
- most_frequent_character_count
> allowed_replacements
):
# Reduce the frequency of the outgoing character.
left_character = input_string[window_start]
character_frequency[left_character] -= 1
# Move the window start forward.
window_start += 1
# Track the maximum window size.
max_length = max(max_length, window_end - window_start + 1)
return max_length
Case | How to Handle |
---|---|
Empty string s | Return 0 if the string is empty, as there's no substring to consider. |
k = 0 and string s has repeating characters | Return the length of the longest substring of repeating characters directly without any replacements. |
k is greater than or equal to the length of s | Return the length of s, since you can replace all characters to have a uniform string. |
String 's' contains only one unique character | Return the length of s, as no replacements are necessary. |
String 's' length is large (approaching maximum string length allowed by the language) | Ensure the sliding window approach is efficient in terms of time and space complexity to prevent timeouts or memory errors. |
k is a large number | The algorithm should still work correctly without integer overflow or performance degradation. |
String 's' contains a mix of almost equal occurrences of several distinct characters | The sliding window needs to dynamically adjust size based on 'k' and character frequencies. |
String with only two distinct characters and a large value of k | The solution should still be able to find the longest substring consisting of one character after replacements. |