Pattern visualizer
Partition Labels
For every character in the string, all of its occurrences must belong to the exact same partition. The greedy insight: if a character appears at index i, the current partition MUST extend at least as far as that character's LAST occurrence in the string. As we scan left to right, we continuously update the required partition boundary to max(end, last[char]). The very first moment our scan index i catches up to end, every character seen so far is completely contained within [start, i], so we can safely cut a partition right there to maximize total parts. Animated on: s = "ababcbacadefegdehijhklij" — partition into as many parts as possible so each letter appears in at most one part..
Greedy farthest-last-occurrence partition cuts
Precompute last indices: 'a'→8, 'b'→5, 'c'→7, 'd'→14, 'e'→15, 'f'→11, etc. Initialize start=0, end=0.
1FUNCTION partitionLabels(s):2 last = a lookup table (char → its last index in s)3 FOR i from 0 to length of s - 1: store s[i] → i in last4 ans = empty list, start = 0, end = 05 FOR i from 0 to length of s - 1:6 end = the larger of end and the last index of s[i]7 IF i equals end:8 append (end - start + 1) to ans9 start = i + 110 RETURN ans
← / → step · space play · Home restart