Skip to content

Commit b0756ac

Browse files
authored
Create partition-labels.py
1 parent 2e1f778 commit b0756ac

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/partition-labels.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def partitionLabels(self, S):
6+
"""
7+
:type S: str
8+
:rtype: List[int]
9+
"""
10+
lookup = {c: i for i, c in enumerate(S)}
11+
first, last = 0, 0
12+
result = []
13+
for i, c in enumerate(S):
14+
last = max(last, lookup[c])
15+
if i == last:
16+
result.append(i-first+1)
17+
first = i+1
18+
return result

0 commit comments

Comments
 (0)