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.
Solutions
Solution 1: Greedy
We first use an array or hash table \(\textit{last}\) to record the last occurrence of each letter in the string \(s\).
Next, we use a greedy approach to partition the string into as many segments as possible.
Traverse the string \(s\) from left to right, while maintaining the start index \(j\) and end index \(i\) of the current segment, both initially set to \(0\).
For each letter \(c\) visited, get the last occurrence position \(\textit{last}[c]\). Since the end index of the current segment must not be less than \(\textit{last}[c]\), let \(\textit{mx} = \max(\textit{mx}, \textit{last}[c])\).
When visiting the index \(\textit{mx}\), it means the current segment ends. The index range of the current segment is \([j,.. i]\), and the length is \(i - j + 1\). We add this length to the result array. Then set \(j = i + 1\) and continue to find the next segment.
Repeat the above process until the string traversal is complete to get the lengths of all segments.
Time complexity is \(O(n)\), and space complexity is \(O(|\Sigma|)\). Where \(n\) is the length of the string \(s\), and \(|\Sigma|\) is the size of the character set. In this problem, \(|\Sigma| = 26\).