Cut the string s into consecutive pieces so that no letter appears in more than one piece: every copy of a letter has to land in the same piece. Reading the pieces in order must give back s.
Make as many pieces as possible, and return their lengths from left to right.
The pieces are zebraz, oo and s. The first piece cannot end sooner, because the second z sits at position 5 (counting from 0). The two os must share a piece, and s stands alone.
m appears once and makes a piece by itself. The first i and the last i are at the two ends of the rest, so everything after m is one piece.
No letter repeats, so every letter is its own piece.
Constraints
1 <= s.length <= 500s is made of lowercase English letters only.Once a piece holds some letter, it has to stretch at least to that letter's last copy in s. A piece can end at position i exactly when no letter in it appears again after i.
Think about the cuts, not the pieces. A cut between two neighbouring letters splits s into a left part and a right part. That cut is allowed when no letter shows up on both sides of it. If some letter does, its copies end up in different pieces, which the rules forbid.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium