-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_repeating_character_replacement.py
29 lines (29 loc) · 1.23 KB
/
longest_repeating_character_replacement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class LongestRepeatingCharacterReplacement:
@staticmethod
def characterReplacement(s: str, k: int) -> int:
# Special case
if s is None or len(s) == 0 or k < 0:
return 0
# Left and right pointers for the sliding window
left, right = 0, 0
# Array to store the frequencies of characters in the string
frequencies = [0] * 26
# Count for most popular character so far
max_count = 0
# Longest length
longest_length = 0
# Process the strings using sliding window
while right < len(s):
# Calculate the frequency of current character
frequencies[ord(s[right]) - ord('A')] += 1
frequency = frequencies[ord(s[right]) - ord('A')]
# Update max_count if required
max_count = max(max_count, frequency)
# If there are more than k characters that are not the same as
# the most popular character, we shift the window
while right - left + 1 - max_count > k:
frequencies[ord(s[left]) - ord('A')] -= 1
left += 1
longest_length = max(longest_length, right - left + 1)
right += 1
return longest_length