Home
DSA Patterns
Greedy Algorithms
Partition Labels
Partition Labels (LeetCode 763)
Partition Labels (LeetCode 763): split a string into as many parts as possible so each letter appears in only one part, by cutting when every letter seen has ended.
Problem Understanding
Partition Labels: split the string s into as many parts as possible so that each letter appears in at most one part, and return the sizes of the parts in order.
Example: "ababcbacadefegdehijhklij" returns [9, 7, 8]: ababcbaca, defegde, hijhklij.
The Intuition: A Part Must Reach Its Letters' Last Copies
If a part contains the letter a, it must also contain the last a in the string, or a would show up in two parts. So a part starting at start has to stretch at least to the last index of every letter inside it.
- Record
last[c]for every letter. - Scan with
end = max(end, last[s[i]]): each new letter may push the part further. - When
i == end, nothing in the part appears later. Cut here and start the next part ati + 1.
Cutting at the first moment it is allowed leaves the most room for later parts, which is why this greedy scan gives the maximum number of parts. It is merging intervals in disguise, with no sort needed.
Partition Labels Visualization
CUT WHEN EVERY LETTER IN THE PART HAS ENDED
LAST INDEX OF EACH LETTER
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
s
last
part
start
0
end
0
sizes
No cuts yet
Record each letter's last index: a→8, b→5, c→7, d→14, e→15, f→11, g→13, h→19, i→22, j→23, k→20, l→21
Next
'a' last appears at 8: the part must stretch to 8
s
Below each letter is the index where it last appears. Watch end (outlined) jump right whenever a letter with a later last copy is scanned, and the part row fill in a whole block at each cut.
Partition Labels Dry Run
Input: "ababcbacadefegdehijhklij"
| Part | Letters | Stretched to | Size |
|---|---|---|---|
| 1 | a, b, c | 8 (last a) | 9 |
| 2 | d, e, f, g | 15 (last e) | 7 |
| 3 | h, i, j, k, l | 23 (last j) | 8 |
The answer is [9, 7, 8].
Partition Labels LeetCode Solution
Last indexes, then one scan that stretches and cuts.
Partition Labels Code
1var partitionLabels = function(s) {2 const last = {};3 for (let i = 0; i < s.length; i++) last[s[i]] = i;4 const sizes = [];5 let start = 0, end = 0;6 for (let i = 0; i < s.length; i++) {7 end = Math.max(end, last[s[i]]);8 if (i === end) {9 sizes.push(end - start + 1);10 start = i + 1;11 }12 }13 return sizes;14};
Partition Labels in Python, Java, C++, TypeScript and C
Partition Labels in Python
class Solution:
def partitionLabels(self, s: str) -> List[int]:
last = {c: i for i, c in enumerate(s)}
sizes, start, end = [], 0, 0
for i, c in enumerate(s):
end = max(end, last[c])
if i == end:
sizes.append(end - start + 1)
start = i + 1
return sizesPartition Labels in Java
class Solution {
public List<Integer> partitionLabels(String s) {
int[] last = new int[26];
for (int i = 0; i < s.length(); i++) last[s.charAt(i) - 'a'] = i;
List<Integer> sizes = new ArrayList<>();
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
end = Math.max(end, last[s.charAt(i) - 'a']);
if (i == end) {
sizes.add(end - start + 1);
start = i + 1;
}
}
return sizes;
}
}Partition Labels in C++
class Solution {
public:
vector<int> partitionLabels(string s) {
int last[26];
for (int i = 0; i < (int)s.size(); i++) last[s[i] - 'a'] = i;
vector<int> sizes;
int start = 0, end = 0;
for (int i = 0; i < (int)s.size(); i++) {
end = max(end, last[s[i] - 'a']);
if (i == end) {
sizes.push_back(end - start + 1);
start = i + 1;
}
}
return sizes;
}
};Partition Labels in TypeScript
function partitionLabels(s: string): number[] {
const last: Record<string, number> = {};
for (let i = 0; i < s.length; i++) last[s[i]] = i;
const sizes: number[] = [];
let start = 0, end = 0;
for (let i = 0; i < s.length; i++) {
end = Math.max(end, last[s[i]]);
if (i === end) {
sizes.push(end - start + 1);
start = i + 1;
}
}
return sizes;
}Partition Labels in C
int* partitionLabels(char* s, int* returnSize) {
int n = strlen(s), last[26];
for (int i = 0; i < n; i++) last[s[i] - 'a'] = i;
int* sizes = malloc(n * sizeof(int));
int start = 0, end = 0;
*returnSize = 0;
for (int i = 0; i < n; i++) {
if (last[s[i] - 'a'] > end) end = last[s[i] - 'a'];
if (i == end) {
sizes[(*returnSize)++] = end - start + 1;
start = i + 1;
}
}
return sizes;
}Edge Cases & Common Mistakes
- Every letter distinct: each character is its own part, all of size 1.
- A letter at both ends: the whole string is one part, as in
"eccbbbbdec". - Using the first occurrence: the part must reach the last copy of each letter, not the first.
- Returning the parts instead of sizes: the problem asks for lengths; compute
end − start + 1.
Summary
Record each letter's last index. Scan once, stretching end to the last copy of every letter met, and cut whenever i reaches end. Each cut is the earliest legal one, which maximises the parts. O(n) time, O(1) extra space.
More practice: DSA interview questions with answers · patterns cheat sheet
