Pack with copy tokens
A packer rewrites a byte stream as a list of tokens. Working from the left, at each position it looks for the longest run of characters starting there that also starts at some earlier position. A copy is allowed to read characters it is itself producing, so a source beginning before the current position may run past it, which is how a long repeat of one character packs into a single token. When that longest run is 2 characters or more the packer emits a copy token of exactly that length and jumps past it. Otherwise it emits a literal token covering the single character at that position and moves on by one. Given the stream, return the length of every token the packer emits, in order. An empty stream emits nothing.
packer_token_lengths(stream: str) → list[int]["aaaaa"]out[1,4]["abcabcabc"]out[1,1,1,6]["abcd"]out[1,1,1,1]State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
["aaaaa"][1,4]not run yetsample["abcabcabc"][1,1,1,6]not run yetsample["abcd"][1,1,1,1]not run yetsample