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 <= 500s 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:
Imagine you have a long sequence of colored beads and you want to chop it into the shortest possible segments so that no color appears in more than one segment. The brute force approach involves testing every single place you could possibly make a cut to see if it successfully traps all matching colors within their respective pieces.
Here's how the algorithm would work step-by-step:
def partition_labels(string_sequence):
partition_sizes = []
current_index = 0
while current_index < len(string_sequence):
end_index = current_index
check_index = current_index
# We expand the boundary whenever we encounter a character that appears later in the string
while check_index <= end_index:
target_character = string_sequence[check_index]
last_seen_position = string_sequence.rfind(target_character)
if last_seen_position > end_index:
end_index = last_seen_position
check_index += 1
# Once no characters in our segment appear outside of it, we record the partition length
partition_length = (end_index - current_index) + 1
partition_sizes.append(partition_length)
current_index = end_index + 1
return partition_sizesThe optimal strategy works by looking ahead to find the absolute last time each letter appears in the message. Armed with this knowledge, we can slice the message into the maximum number of independent chunks where no letter appears in more than one chunk.
Here's how the algorithm would work step-by-step:
def partition_labels(message_string):
last_occurrence_map = {}
# Record the final index of every character to know how far each partition must extend.
for index, character in enumerate(message_string):
last_occurrence_map[character] = index
partition_sizes_list = []
current_partition_start = 0
furthest_required_stop = 0
for index, character in enumerate(message_string):
# Expand the boundary if the current character appears later in the message string.
if last_occurrence_map[character] > furthest_required_stop:
furthest_required_stop = last_occurrence_map[character]
# Finalize the partition when our scanning position reaches the required stopping boundary.
if index == furthest_required_stop:
partition_length = furthest_required_stop - current_partition_start + 1
partition_sizes_list.append(partition_length)
current_partition_start = index + 1
return partition_sizes_list| Case | How to Handle |
|---|---|
| Empty string input | Return an empty list immediately because no partitions can be formed from a zero-length string. |
| String with a single character | Return a list containing the integer 1 since the single character forms a partition of size one. |
| String with all identical characters like aaaaa | Return a list containing the total length of the string because all occurrences must fall within a single partition. |
| String with all unique characters like abcde | Return a list of ones matching the string length because each character can be partitioned independently. |
| Maximum length constraints up to the problem limit | Use a single-pass frequency pre-computation and a linear greedy scan to ensure optimal O(n) time complexity and prevent timeouts. |
| Alternating characters like ababcbacadefegde | Extend the current partition boundary dynamically using the last occurrence index until the pointer catches up to the max boundary. |
| String containing uppercase letters or mixed types | Assuming standard lowercase English per constraints, but if mixed, sanitize or map characters using a sized array of 26 or a hash map. |
| Memory limits during large string processing | Store only the last seen indices in a fixed-size array of 26 integers to maintain O(1) auxiliary space complexity. |