You are given a string s
. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc"
can be partitioned into ["abab", "cc"]
, but partitions such as ["aba", "bcc"]
or ["ab", "ab", "cc"]
are invalid.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s
.
Return a list of integers representing the size of these parts.
Example 1:
Input: s = "ababcbacadefegdehijhklij" Output: [9,7,8] Explanation: The partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
Example 2:
Input: s = "eccbbbbdec" Output: [10]
Constraints:
1 <= s.length <= 500
s
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for partitioning labels involves exploring every single possible way to divide the given string. We will check each possible partition to see if it meets the problem's requirement that each letter appears in only one partition. Ultimately, we want to find all valid partitions and select the shortest ones.
Here's how the algorithm would work step-by-step:
def partition_labels_brute_force(input_string):
partition_sizes = []
start_index = 0
while start_index < len(input_string):
end_index = start_index
# Expand the partition to include all occurrences of the character
last_occurrence = input_string.rfind(input_string[start_index])
end_index = max(end_index, last_occurrence)
current_index = start_index + 1
while current_index < end_index:
# Find if any character in the current partition extends it
last_occurrence = input_string.rfind(input_string[current_index])
end_index = max(end_index, last_occurrence)
current_index += 1
#This is the end of loop for extending partition
#Record partition size after expansion
partition_sizes.append(end_index - start_index + 1)
start_index = end_index + 1
return partition_sizes
The key to this puzzle is figuring out how far each letter stretches in the string. We efficiently group letters together by making sure all occurrences of each letter are within the same group, creating the largest possible groups from left to right.
Here's how the algorithm would work step-by-step:
def partition_labels(input_string):
last_occurrence = {}
for index, char in enumerate(input_string):
last_occurrence[char] = index
partition_sizes = []
start_index = 0
end_index = 0
for index, char in enumerate(input_string):
# Extend the end of the current partition
end_index = max(end_index, last_occurrence[char])
if index == end_index:
# Found a complete partition
partition_size = end_index - start_index + 1
partition_sizes.append(partition_size)
start_index = index + 1
return partition_sizes
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list immediately, as there are no partitions to create. |
String with only one character | Return a list containing only the length of the string (which is 1) because the single character is its own partition. |
String where all characters are identical | Return a list containing only the length of the entire string, since the whole string is the only possible partition. |
String with maximum possible length (e.g., close to memory limits) | Ensure the chosen data structures (e.g., hash map for last occurrence) can accommodate the size, and the algorithm's time complexity remains efficient (ideally O(n)). |
String where one character appears only at the beginning | The partition will extend to include the last occurrence of every character within the initial single character's range, correctly encompassing all necessary characters. |
String where one character appears only at the end | The partition will expand backward to include all characters relevant to the partition which will work correctly, given how partitions are found. |
Overlapping partition ranges requiring merging | The greedy approach will correctly merge the overlapping ranges by always extending current partition to the furthest right position of any character within the current partition. |
String with a very large number of unique characters | The hash map may consume a considerable amount of memory, but the algorithm should still function correctly if sufficient memory is available. |