In the previous part, the stop token "<END>" always fit fully inside a single chunk. Now it may be split across two or more consecutive chunks: for example, one chunk could end with "<E" while the next begins with "ND>", together forming "<END>".
This adds two new rules:
- A chunk's trailing characters that could be the start of
"<END>"must be held back instead of emitted right away, until later characters confirm or rule out the stop token. - Output must be produced as early as possible: emit each chunk's safe content as soon as it is known, without waiting for the full stream.
A held-back suffix still belongs to the chunk it came from, not to the chunk that later resolves it:
- If it's ruled out, whether by later characters that don't match or by the stream ending, it's appended to its origin chunk's output.
- The chunk that resolves it still gets its own output for its own new content, so every input chunk still maps to at most one output chunk.
Constraints:
- All constraints from the previous question still apply.
Example 1:
Input: stream = ["hello", "how are you<E", "ND>HAPPY"] Output: ["hello", "how are you"] Explanation:
- The first chunk "hello" contains no stop token and no trailing partial match, so it is emitted unchanged.
- The second chunk "how are you<E" ends with "<E", which is a prefix of the stop token "
" so "how are you" is emitted and "<E" is held back as a potential stop token. - The third chunk "ND>HAPPY" combines with the held "<E" to form "<END>HAPPY"; the stop token "
" is confirmed at the start, so nothing further is emitted and the remaining text is discarded.
Example 2:
Input: stream = ["start<E", "x middle", " end<END>drop"] Output: ["start<E", "x middle", " end"]
Example 3:
Input: stream = ["abc<E"] Output: ["abc<E"]
Accepted
239/727
Acceptance Rate32.9%
Ask AI Assistant

Ask AI Assistant
Ask me anything about this coding question!
Test Cases
Input
stream =
Output
Expected