Example 1
- Input
- s = "ababcbacadefegdehijhklij"
- Output
- [9,7,8]
The parts are "ababcbaca", "defegde" and "hijhklij", with sizes 9, 7 and 8. Splitting into fewer parts, such as merging the first two, would still be valid but not maximal.
You are given a string and must split it into as many contiguous parts as possible so that no letter ever appears in more than one part. Return the length of each part in order.
You are given a string s. Partition s into as many parts as possible so that each letter appears in at most one part.
The parts must be contiguous and, concatenated in order, must reconstruct s. Return a list of integers holding the size of each part.
1 <= s.length <= 500s consists of lowercase English letters.The parts are "ababcbaca", "defegde" and "hijhklij", with sizes 9, 7 and 8. Splitting into fewer parts, such as merging the first two, would still be valid but not maximal.
The letters e and c appear at both the start and the end, so the whole string is one part of size 10.
One character is already one valid part. There is no boundary inside it, so the maximum number of parts is one.
The first a forces the range through index 2, but b at index 1 reappears at 4 and extends it to abacb. Letters d, e and f each occur once and form separate parts. Cutting after aba would split the two b occurrences.
Every occurrence of a letter must remain in one part. Seeing a at index 0 in Example 1 therefore forces the part through its last occurrence at 8. Letters discovered inside that forced range may extend it again. The first position where no encountered letter reaches farther is the earliest legal cut; taking every such cut produces as many parts as possible.
Reveal Repeated Last Search: intuition, complexity, and code
e must extend past the last occurrence of every letter inside it."abc", split into all ones: [1, 1, 1].e in "eccbbbbdec", forces one part covering the whole string.end + 1 after a cut, which double-counts the boundary character.When each item pulls in everything linked to it, precomputing the farthest reach of every item turns repeated expansion into one greedy sweep.