Leetcode 763. Partition Labels
1 min readMay 7, 2021
[medium][Amazon]
A string S
of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and 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.
Note:
S
will have length in range[1, 500]
.S
will consist of lowercase English letters ('a'
to'z'
) only.
[Think]
- need to understand the question, partition the string at most possible
- update all alphbet last appearance, a substring need to include all its index
- Therefore, create two pointers, first one note the begining, another one note the last appearance index. Once we scan through the string, if there is a bigger index pop out, will need to update the last index.
- Be careful for the add need to be “start + last -1”; and start = last+1;