Skip to content

Commit a727dca

Browse files
committed
Time: 3 ms (91.42%), Space: 17.7 MB (54.42%) - LeetHub
1 parent ff6acca commit a727dca

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# time complexity: O(n)
2+
# space complexity: O(1)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def partitionLabels(self, s: str) -> List[int]:
8+
lastIdx = {c: i for i, c in enumerate(s)}
9+
right = 0
10+
left = 0
11+
result = []
12+
for i, c in enumerate(s):
13+
right = max(right, lastIdx[c])
14+
if i == right:
15+
result.append(right - left + 1)
16+
left = right + 1
17+
return result
18+
19+
20+
'''
21+
ababcbacadefegdehijhklij
22+
{'a': 8, 'b': 5, 'c': 7, 'd': 14, 'e': 15, 'f': 11,
23+
'g': 13, 'h': 19, 'i': 22, 'j': 23, 'k': 20, 'l': 21}
24+
25+
5 == 5
26+
result.append
27+
'''
28+
s = "ababcbacadefegdehijhklij"
29+
print(Solution().partitionLabels(s))
30+
s = "eccbbbbdec"
31+
print(Solution().partitionLabels(s))

0 commit comments

Comments
 (0)