Back to Practice
#0302
Partition Labels
MediumDSA28 min20 XP
Problem
Return the sizes of as many partitions as possible so each character appears in at most one partition.
Why This Matters
Partition Labels is a compact greedy interval problem over character last positions.
Function Signature
def partition_labels(s):
Examples
Example 1
Inputs = "ababcbacadefegdehijhklij"
Output[9, 7, 8]
The first partition must include every a, b, and c, ending at index 8.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic labels
Input"ababcbacadefegdehijhklij"
Expected[9, 7, 8]
The string splits into three maximal safe parts.
All unique
Input"abc"
Expected[1, 1, 1]
Each character can be its own partition.
Hidden Test Categories
all same characterempty stringtwo repeated groups