The goal is to maximize the number of partitions while ensuring that no character appears in more than one partition.
Problem
You are given a strings. Partition the string into as many parts as possible so that each letter appears in at most one part.
For example, if a character appears in one partition, it must not appear in any other partition. Return a list of integers representing the lengths of these partitions.
Example 1
Input
s = "ababcbacadefegdehijhklij"
Output
[9,7,8]
Explanation
The string can be partitioned into three parts:"ababcbaca" | "defegde" | "hijhklij"
The first partition contains all occurrences of the letters a, b, and c. The second contains all occurrences of d, e, f, and g.
The third contains all occurrences of the remaining letters. The lengths of these partitions are
9, 7, and 8.
Example 2
Input
s = "eccbbbbdec"
Output
[10]
Explanation
All characters must belong to the same partition because the characterse and c appear at both the beginning and the end of the string.
Therefore, the entire string forms a single partition of length
10.
Solution
This problem can be solved using a Greedy Approach. The key observation is that every character must be contained entirely within a single partition. Therefore, when we encounter a character, we must extend the current partition at least until the last occurrence of that character.First, we calculate the last occurrence of every character in the string. Since the string contains lowercase English letters, we can use an integer array of size
26 to store the last index of each character.
Next, we traverse the string from left to right while maintaining two variables:
start: the starting index of the current partition.
end: the farthest index that the current partition must reach to include every occurrence of the characters encountered so far.
For each character at index
i, we update end using the last occurrence of that character. This ensures that the current partition includes all occurrences of every character encountered within it.
When
i == end, we have reached the last required position for all characters in the current partition. No character in this partition appears later in the string, so we can safely close the partition and add its length to the result.
After recording the length, we move
start to i + 1 to begin the next partition. This greedy strategy produces the maximum number of partitions because we close each partition as soon as it is safe to do so.
import java.util.ArrayList;
import java.util.List;
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> result = new ArrayList<>();
int start = 0;
int end = 0;
for (int i = 0; i < s.length(); i++) {
end = Math.max(
end,
last[s.charAt(i) - 'a']
);
if (i == end) {
result.add(end - start + 1);
start = i + 1;
}
}
return result;
}
}
Complexity
Letn be the length of the string. We traverse the string once to calculate the last occurrence of each character and once more to determine the partitions. Each operation takes constant time, so the overall time complexity is O(n).
The last-occurrence array has a fixed size of
26, requiring O(1) auxiliary space. The result list stores the partition lengths and requires up to O(n) space.
Therefore, the total space complexity, including the output, is
O(n), while the auxiliary space complexity excluding the output is O(1).
Comments
No comments yet. Ask a question or share what helped.