Leetcode 763. Partition Labels

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.
  • S will have length in range [1, 500].
  • S will consist of lowercase English letters ('a' to 'z') only.
  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;

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gary Chiang

Gary Chiang

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