Partition Labels
Cut a string into as many parts as possible so that no letter appears in two parts.
Given a string ss
Example 1
Output: [9, 7, 8]
Explanation: the parts are "ababcbaca", "defegde" and "hijhklij".
Example 2
Output: [10]
Explanation: the string starts and ends with e, so it cannot be split.
The Idea
Picture a ribbon printed with the letters of sababcbacadefegdehijhklij
Why not cut right after the first two letters, ab
Is reaching index 8 enough? Every letter picked up on the way brings its own last copy, which could lie further on. Here the last b is at index 5 and the last c at index 7, both before 8, so nothing pushes the part further. At index 8, every letter in the part has used up all its copies, and a cut is allowed. So the real question is: while walking along the string, how far must the current part reach?
Look at the picture below. Under the string, each letter has a bar from its first copy to its last copy.
The orange bar, a: 0 to 8d: 9 to 14e: 10 to 15
So the end of the current part is the farthest last index among the letters it holds so far, and the part can close when the walk reaches that end. Cutting there never leaves fewer parts in the end. No split can cut earlier, because some letter inside would still have a copy after the cut, and everything after the cut is free to be split on its own.
Cutting the ribbon at the first place a cut is allowed, without trying later places first, is called a greedy choice: it takes what looks best right now and never comes back to it.
Solution Approach
Turning that idea into code means settling three things: how to find each letter's last index quickly, how to track the end of the current part, and when to cut.
Each letter's last index
Where does a letter appear for the last time? Calling s.lastIndexOf(c)
Instead, one pass before the walk fills an array last'a'
The end of the current part
How far must the current part reach? A variable endendendend
When to cut
When is the part complete? When the walk's index equals endstartend - start + 1
In Example 2, eccbbbbdecend
Java Solution
The method partitionLabels
- First, fill the array
with a loop overlast : for each indexs , seti tolast[s.charAt(i) - 'a'] .i - Create the list
for the answer, and two variables set to 0:sizes , where the current part begins, andstart , how far it must reach.end - Walk the string again with a second loop over
. At each index, seti toend .Math.max(end, last[s.charAt(i) - 'a']) - If
equalsi , addend toend - start + 1 and setsizes tostart .i + 1 - After the second loop, return
.sizes
The main
import java.util.ArrayList;
import java.util.List;
public class PartitionLabels {
public static List<Integer> partitionLabels(String s) {
int[] last = new int[26];
for (int i = 0; i < s.length(); i++) {
last[s.charAt(i) - 'a'] = i; // a later copy overwrites an earlier one
}
List<Integer> sizes = new ArrayList<>();
int start = 0;
int end = 0; // the current part must reach at least here
for (int i = 0; i < s.length(); i++) {
end = Math.max(end, last[s.charAt(i) - 'a']);
if (i == end) { // every letter seen so far ends in this part
sizes.add(end - start + 1);
start = i + 1;
}
}
return sizes;
}
public static void main(String[] args) {
System.out.println(partitionLabels("ababcbacadefegdehijhklij"));
System.out.println(partitionLabels("eccbbbbdec"));
System.out.println(partitionLabels("a"));
System.out.println(partitionLabels("abc"));
System.out.println(partitionLabels("abcba"));
System.out.println(partitionLabels("aabbccdd"));
}
}
Running it prints:
[9, 7, 8]
[10]
[1]
[1, 1, 1]
[5]
[2, 2, 2, 2]
[9, 7, 8][10]"a"[1]"abc"iend[1, 1, 1]"abcba"end[5][2, 2, 2, 2]
Why Math.maxend = last[s.charAt(i) - 'a']endend[6, 2, 1, 3, 2, 1, 1, 4, 1, 1, 1, 1]
Complexity
The work is two walks over the string, with an array of 26 cells on the side.
- Time: O(n), because the first loop visits each of the n letters of
once to fills , and the second visits each again with one lookup and one comparison.last - Space: O(1) besides the output, because
has 26 cells however longlast is, ands has at most 26 entries since no letter appears in two parts.sizes
Each bar in the picture is an interval, a stretch from a first index to a last index, and each part is a group of overlapping bars. Joining overlapping intervals is the whole task in