-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlongest_repeating_character_replacement.py
47 lines (35 loc) · 1.88 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Question: https://leetcode.com/problems/longest-repeating-character-replacement/
"""
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
"""
Sliding window approach with two pointers.
The pointers start from element 0 and right pointer is moved right everytime until
we meet the condition -> number of replacements required in current window <= k
Here, the number of required to replace should be less than the max allowed replacements (i.e. k)
calculated using -> (window_size - count of most occuring character in the window)
Time complexity: O(N)
"""
# Dict `count` to store the frequency of the characters in the current window
count = {}
max_len = 0
# Left pointer that stays
l = 0
# Right pointer moves to the right, expanding the sliding window
for r, curr_char in enumerate(s):
# Increment the frequency of the current character by 1
count[curr_char] = 1 + count.get(curr_char, 0)
# Check if the `(windowLen - max occuring char in the window)` is <= k
# i.e. check if this is a valid window maximizing the sequence length
# Using this we check if the required replacements are
# less than the allowed 'k' replacements
while (r - l + 1) - max(count.values()) > k:
# If not, then move the left pointer to the right (and decrement the
# count of the char that was taken out of the window by moving
# the left pointer) and make the window valid again
count[s[l]] -= 1
l += 1
# Update the max_len by comparing with current window size
max_len = max(max_len, r-l+1)
return max_len