Code RoomPack with copy tokens
MediumPrep Room Coding #4935

Pack with copy tokens

CodingAlgorithms & data structuresMid–Senior~30 min

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.

Implement
packer_token_lengths(stream: str) → list[int]
Examples
in["aaaaa"]out[1,4]
in["abcabcabc"]out[1,1,1,6]
in["abcd"]out[1,1,1,1]
What a strong answer looks like

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.

0:00 of about 30 min
InputExpectedGot
["aaaaa"][1,4]not run yetsample
["abcabcabc"][1,1,1,6]not run yetsample
["abcd"][1,1,1,1]not run yetsample