763. Partition Labels
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 81% Topics: Hash Table, Two Pointers, String, Greedy
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def partitionLabels(self, S):
"""
:type S: str
:rtype: List[int]
"""
lookup = {c: i for i, c in enumerate(S)}
first, last = 0, 0
result = []
for i, c in enumerate(S):
last = max(last, lookup[c])
if i == last:
result.append(i-first+1)
first = i+1
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions