A string S of lowercase 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 letters (‘a’ to ‘z’) only.
Problem URL
给一个全部由小写字母构成的String,将它分为多个部分,使得所有字母都只出现一次。
Using two pass iteration to solve this problem. First pass stores the last postion of each letter occurs. Second pass find the max index of the chars, when come to this position, split and count length using two pointers.
Time Complexity: O(n) Space Complexity: O(1)