forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0763_Partition_Labels.py
More file actions
27 lines (24 loc) · 886 Bytes
/
Copy path0763_Partition_Labels.py
File metadata and controls
27 lines (24 loc) · 886 Bytes
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
class Solution:
def mergeOverlappingSpans(self, spans):
spans = sorted(spans)
i, j = 0, 1
while j < len(spans):
if spans[i][1] < spans[j][0]: # Non Overlapping
i += 1
spans[i] = spans[j]
else:
spans[i][1] = max(spans[i][1], spans[j][1])
j += 1
return spans[:i+1]
def partitionLabels(self, S: str) -> List[int]:
spans = {}
for i in range (len(S)):
if S[i] not in spans:
spans[S[i]] = [i, i]
else:
spans[S[i]][1] = i
# one span [startIndex, endIndex] for each unique character
# there will be maximum 26 spans
spans = spans.values()
nonOverlappingSpans = self.mergeOverlappingSpans(spans)
return [span[1]-span[0]+1 for span in nonOverlappingSpans]