You are given a string s and need to partition it into as many parts as possible while ensuring each letter appears in at most one part.
Think of this as cutting a string into segments where no character appears in multiple segments. For example, with the string "ababcbacadefegdehijhklij", you need to find the optimal cutting points.
Goal: Return a list of integers representing the size of each part after partitioning.
Example: "ababcbaca" can be partitioned as ["ababcbaca"] (size 9) because 'a', 'b', and 'c' are all intertwined throughout the string. But "ababccdd" could be partitioned as ["abab", "cc", "dd"] (sizes [4, 2, 2]).
Input & Output
Visualization
Time & Space Complexity
Two passes through the string: O(n) + O(n) = O(n)
Hash map stores at most 26 entries for lowercase letters, which is constant space
Constraints
- 1 โค s.length โค 500
- s consists of lowercase English letters only
- Each character appears at least once