September LeetCoding Challenge, Day 4: Partition Labels
September 5, 2020The problem for September 4 is Partition Labels. According to the
problem statement, we have a string \(S\) of lowercase English characters which
we want to partition in as many parts as possible. The catch is that each letter
must appear in at most one part. Each part is a substring, not a subsequence of
string \(S\). For example, given string \(S\) equal to
"ababcbacadefegdehijhklij"
, the valid partition that produces most parts is
"ababcbaca"
, "defegde"
and "hijhklij"
.
Once we select a given character to belong to a part, then all characters with the same letter as the chosen character must belong to that part. In sum, each part must go as far as the last occurrence of each letter in the part. We can solve this in \(\mathcal{O}(|S|)\) by first identifying the indices of the last occurrences of each letter, and then greedily collect characters for each part until the previous restriction is satisfied. The following is an implementation of that strategy: