We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 22fa69e commit 7bb22e8Copy full SHA for 7bb22e8
14. Questions/leetcode 424 - longest repeating character replacement.py
@@ -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