Sign in

[Java][Greedy][LeetCode] PartitionLabels #763

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.

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.

Array fast than HashMap.
Recording the last position of every letter and saving in Array or HashMap.
Sweeping string of input again. setting the end point is the maximum. when the index position is equal to the end point, it means that an interval ends. calculate the difference with start point and end point.
先針對每個字母去紀錄最後一次出現的位置,將位置存在Array或是HashMap,
再掃一次字串,將結束點設為最大值,當索引位置等於結束點,則表示一個區間結束並將起始點設在結束點的後面一的地方。