Skip to content

Commit 7bb22e8

Browse files
committedFeb 6, 2023
added: longest repeating character replacement
1 parent 22fa69e commit 7bb22e8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# longest repeating character replacement | leetcode 424 | https://leetcode.com/problems/longest-repeating-character-replacement/
2+
# keep track of max freq in sliding window and check if size of window - max freq > k
3+
4+
class Solution:
5+
def characterReplacement(self, s: str, k: int) -> int:
6+
ptrL = 0
7+
ptrR = 0
8+
longest = 0
9+
freq = dict()
10+
max_freq = 0
11+
w_size = 0
12+
13+
for ptrR in range(len(s)):
14+
freq[s[ptrR]] = 1 + freq.get(s[ptrR], 0)
15+
max_freq = max(max_freq, freq[s[ptrR]])
16+
17+
if (ptrR - ptrL + 1) - max_freq > k:
18+
freq[s[ptrL]] -= 1
19+
ptrL += 1
20+
21+
longest = max(longest, (ptrR - ptrL + 1))
22+
23+
return longest

0 commit comments

Comments
 (0)
Failed to load comments.