-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlongest.py
37 lines (33 loc) · 1.13 KB
/
longest.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
from collections import defaultdict
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
"""Calculate the longer repeating character
string when replacing with one letter
Args:
s (str): String to evaluate
k (int): Number of characters to replace
Returns:
int: Length of longest repeating sequence
"""
behind, ahead = 0, 0
char_count = defaultdict(int)
char_count[s[ahead]] += 1
max_length = 0
while ahead < len(s):
length = ahead - behind + 1
max_freq = max(char_count.values())
if length - max_freq <= k:
max_length = max(length, max_length)
ahead += 1
if ahead == len(s):
break
char_count[s[ahead]] += 1
else:
char_count[s[behind]] -= 1
behind += 1
return max_length
# Checking in terminal/console:
if __name__ == '__main__':
Sol = Solution()
Solve = Sol.characterReplacement(s = "ABAB", k = 2 ) # s = "ABAB", k = 2 -> 4
print(Solve)