-
Notifications
You must be signed in to change notification settings - Fork 17
/
partition-labels.py
33 lines (24 loc) · 986 Bytes
/
partition-labels.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
from collections import defaultdict
class Solution:
def partitionLabels(self, S: str) -> List[int]:
string_to_interval = defaultdict(lambda: [float("+inf"),0])
def get_intervals(S):
for pos, char in enumerate(S):
string_to_interval[char][0] = min(
pos,
string_to_interval[char][0]
)
string_to_interval[char][1] = pos
def merge_intervals(intervals):
result = [[0, 0]]
for new_begin, new_end in intervals:
begin, end = result[-1]
if new_begin <= end:
end = max(new_end, end)
result[-1] = [begin, end]
else:
result.append([new_begin, new_end])
return result
get_intervals(S)
intervals = merge_intervals(sorted(string_to_interval.values()))
return [i[1] - i[0] + 1 for i in intervals]