-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
48 lines (36 loc) · 1.15 KB
/
Solution.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
48
"""
Given a string s that consists of only uppercase English letters, you can perform at most k operations on that string.
In one operation, you can choose any character of the string and change it to any other uppercase English character.
Find the length of the longest sub-string containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2
Output:
4
Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1
Output:
4
Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
"""
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
import collections
res = lo = hi = 0
counts = collections.Counter()
for hi in range(1, len(s)+1):
counts[s[hi-1]] += 1
max_char_n = counts.most_common(1)[0][1]
if hi - lo - max_char_n > k:
counts[s[lo]] -= 1
lo += 1
return hi - lo