Leetcode 763. Partition Labels

[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]

  1. need to understand the question, partition the string at most possible
  2. update all alphbet last appearance, a substring need to include all its index
  3. 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.
  4. Be careful for the add need to be “start + last -1”; and start = last+1;

CS new grad, 6 years experience related to supply chain management. Located in Bay area