-
Notifications
You must be signed in to change notification settings - Fork 0
/
424_longest_rep_char_replacement.py
85 lines (65 loc) · 2.08 KB
/
424_longest_rep_char_replacement.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
424. Longest Repeating Character Replacement
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.
def characterReplacement(self, s: str, k: int) -> int:
'''
from collections import Counter
class Solution(object):
def characterReplacement(self, s, k):
'''
TC: O(N)
SC: O(N)
approach 1:
sliding window
window ranges from index start to len(s)
look at chars in this window and keep track of counts
using dict
'''
count = {}
max_count = start = result = 0
for end in range(len(s)):
count[s[end]] = count.get(s[end], 0) + 1
# add 1 to char count or set to 0
max_count = max(max_count, count[s[end]])
if end - start + 1 - max_count > k:
count[s[start]] -= 1
start += 1
result = max(result, end-start+1)
return result
'''
approach 2:
using counter
'''
start = res = 0
count = Counter()
for end in range(len(s)):
count[s[end]] += 1
max_count = count.most_common(1)[0][1]
if end - start + 1 - max_count > k:
count[s[start]] -= 1
start += 1
res = max(res, end-start+1)
return res
if __name__ == '__main__':
# begin
s = Solution()
print(s.characterReplacement("ABAB", 2))