Partition Labels
Medium
Topics
You are given a string s. Partition it into as many parts as possible so that each letter appears in at most one part. Return a list of the sizes of these parts, in order.
Example 1
No letter may appear in two parts, so a part cannot end before the last occurrence of every letter it contains.
Shades mark the partitions, left to right.
Input: s = "ababcbacadefegdehijhklij" Output: [9,7,8]
Example 2
Input: s = "eccbbbbdec" Output: [10]
Constraints
- 1 <= s.length <= 500
- s consists of lowercase English letters.
Editorial — intuition, approach, complexity and pitfalls
Intuition
A part must extend at least until the last occurrence of every letter it contains. Track the farthest last-occurrence as you scan; when the current index reaches that boundary, the part is complete.
Approach
- Record the last index of each letter.
- Scan with a running
end = max(end, last[s[i]]). - When
i == end, cut a part of lengthi - start + 1and reset start.
Dry Run
s = "ababcbacadefegdehijhklij"
First pass records the LAST index of every character. Second pass walks forward,
carrying `end` as the furthest index any character seen in this block reaches:
i=0 a, last a is 8 end 0 -> 8
i=1..7 end stays 8
i=8 i == end CUT, block length 9 res [9]
i=9 d, last d is 14 end 8 -> 14
i=10 e, last e is 15 end 14 -> 15
i=11..14 end stays 15
i=15 i == end CUT, block length 7 res [9, 7]
i=16 h, last h is 19 end 15 -> 19
i=17 i, last i is 22 end 19 -> 22
i=18 j, last j is 23 end 22 -> 23
i=19..22 end stays 23
i=23 i == end CUT, block length 8 res [9, 7, 8]
answer 9 7 8
`end` is a running MAXIMUM over the block, not the last index of the current
character. That distinction is the whole algorithm: reaching i == end means
nothing before i reappears after i, which is exactly the condition for a cut.
Cut on last[c] instead and the splits come far too early -- this input returns
6 2 1 3 2 1 1 4 1 1 1 1 instead of 9 7 8.
The first pass is what makes the greedy second pass safe. Knowing every last
index up front means the cut decision at i is final and never needs revisiting,
which is why one forward walk suffices with no backtracking.Complexity
- Time — O(n): one pass to record last indices, one pass to partition.
- Space — O(1): the last-index map holds at most 26 entries.
Common Pitfalls
- The cut is at
i == end, whereendis the running maximum last-index over everything seen in this block -- not the last index of the current character. Cutting onlast[c]splits far too eagerly, returning6 2 1 3 2 1 1 4 1 1 1 1where the answer is9 7 8. - One pass to record last positions, one to walk. The first pass is what makes the second greedy choice safe.
Full Solution — all languages
class Solution:
def partitionLabels(self, s):
last={c:i for i,c in enumerate(s)}
res=[]
start=end=0
for i,c in enumerate(s):
end=max(end,last[c])
if i==end:
res.append(i-start+1)
start=i+1
return resimport java.util.*;
class Solution {
public java.util.List<Integer> partitionLabels(String s) {
int[] last=new int[128];
for(int i=0;i<s.length();i++) last[s.charAt(i)]=i;
List<Integer> res=new ArrayList<>();
int start=0,end=0;
for(int i=0;i<s.length();i++) {
end=Math.max(end,last[s.charAt(i)]);
if(i==end) {
res.add(i-start+1);
start=i+1;
}
}
return res;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> partitionLabels(string s) {
int last[128];
for(int i=0;i<(int)s.size();i++) last[(int)s[i]]=i;
vector<int> res;
int start=0,end=0;
for(int i=0;i<(int)s.size();i++) {
end=max(end,last[(int)s[i]]);
if(i==end) {
res.push_back(i-start+1);
start=i+1;
}
}
return res;
}
};/**
* @param {string} s
* @return {number[]}
*/ var partitionLabels = function(s) {
const last={};
for(let i=0;i<s.length;i++) last[s[i]]=i;
const res=[];
let start=0,end=0;
for(let i=0;i<s.length;i++) {
end=Math.max(end,last[s[i]]);
if(i===end) {
res.push(i-start+1);
start=i+1;
}
}
return res;
};