-
Notifications
You must be signed in to change notification settings - Fork 17
/
longest-repeating-character-replacement.py
113 lines (82 loc) · 3.54 KB
/
longest-repeating-character-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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from collections import defaultdict
from functools import cache
from typing import DefaultDict, List
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
def build_char_map() -> DefaultDict[str, List[int]]:
result: DefaultDict[str, List[int]] = defaultdict(list)
for pos, char in enumerate(s):
result[char].append(pos)
return result
char_map: DefaultDict[str, List[int]] = build_char_map()
max_length = 0
for positions in char_map.values():
# Sliding window algorithm
left, right = 0, 0
length = 1
replacements = k
max_length = max(max_length, 1 + min(replacements, len(s) - 1))
while right < len(positions) - 1:
diff = positions[right + 1] - positions[right] - 1
if replacements - diff >= 0:
length += diff + 1
replacements -= diff
max_length = max(
max_length,
length
+ min(
replacements,
len(s) - positions[right + 1] - 1 + positions[0],
),
)
right += 1
elif left == right:
left += 1
right += 1
else:
diff = positions[left + 1] - positions[left] - 1
length -= diff + 1
replacements += diff
left += 1
return max_length
def characterReplacementDPOptimized(self, s: str, k: int) -> int:
# FIXME: incorrect (doesn't take into account ABBB, 1 -> 4)
def build_char_map() -> DefaultDict[str, List[int]]:
result: DefaultDict[str, List[int]] = defaultdict(list)
for pos, char in enumerate(s):
result[char].append(pos)
return result
@cache
def dp(char: str, pos: int, left: int) -> int:
max_length_local = max(
1, min(len(s) - positions[pos] + positions[0], left + 1)
)
for next_pos in range(pos + 1, len(positions)):
diff = positions[next_pos] - positions[pos] - 1
if diff <= left:
max_length_local = max(
max_length_local, dp(char, next_pos, left - diff) + 1 + diff
)
return max_length_local
char_map: DefaultDict[str, List[int]] = build_char_map()
max_length = 0
for char, positions in char_map.items():
max_length = max(
max_length, max(dp(char, pos, k) for pos in range(len(positions)))
)
return max_length
def characterReplacementDP(self, s: str, k: int) -> int:
# FIXME: incorrect (doesn't take into account ABBB, 1 -> 4)
@cache
def dp(pos: int, left: int) -> int:
if len(s) == pos:
return left
max_length = max(1, min(left + 1, len(s) - pos))
for next_pos in range(pos + 1, min(pos + left + 2, len(s) + 1)):
if next_pos == len(s) or s[pos] == s[next_pos]:
replacements = next_pos - pos - 1
max_length = max(
max_length, dp(next_pos, left - replacements) + replacements + 1
)
return max_length
return min(max(dp(pos, k) for pos in range(len(s))), len(s))