-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path424.py
More file actions
23 lines (18 loc) · 784 Bytes
/
424.py
File metadata and controls
23 lines (18 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
charCounts = {}
p1 = 0
maxRepeated = 0 # check neetcode video for reexplanation on why
longestRCR = 0
for p2 in range(len(s)):
if (s[p2] not in charCounts):
charCounts[s[p2]] = 1
else:
charCounts[s[p2]] += 1
contestCounter = charCounts[s[p2]] # new contestant for most repeated
maxRepeated = max(maxRepeated, charCounts[s[p2]]) # tricky on why we can do this
while (k < ((p2 - p1) + 1) - maxRepeated):
charCounts[s[p1]] -= 1
p1 += 1
longestRCR = max(longestRCR, ((p2 - p1) + 1))
return longestRCR