We use a hash map to record the last occurrence of each character. Then we iterate through the string and maintain a boundary which is the furthest last occurrence of any character seen in the current partition.
last = {char: i for i, char in enumerate(s)}.start = 0,end = 0,res = [].- For
i, charinenumerate(s):end = max(end, last[char]).- If
i == end:- Partition found! Append
end - start + 1tores. start = i + 1.
- Partition found! Append
- Return
res.
- Time Complexity: O(N).
- Space Complexity: O(1) (size of alphabet).
def partition_labels(s):
last = {c: i for i, c in enumerate(s)}
start = end = 0
res = []
for i, c in enumerate(s):
end = max(end, last[c])
if i == end:
res.append(end - start + 1)
start = i + 1
return res