← 返回 microsoft 的题目列表Streaming Stop Token: Return Prefix Before First Occurrence (Stop Token May Split Across Chunks)
类型:online_judge
Problem: Find a Stop Token in a Streaming String and Return the Prefix ASAP
You are given an ordered stream of string chunks and a stop_token (a string). Starting from the beginning of the stream, you conceptually concatenate incoming chunks. As soon as the concatenated data contains the first occurrence of stop_token, you must immediately return all characters before that occurrence (excluding the stop_token).
Key requirements:
The stop_token may be split across chunk boundaries.
You must be able to return quickly once the token is detected; you cannot wait to buffer the entire stream and then search.
Return the prefix before the first occurrence.
Input (you may define an appropriate interface)
chunks: an ordered sequence of strings (e.g., List[str] or an iterator), each element is a chunk.
stop_token: a string.
Output
A string: all characters from the start up to (but not including) the first occurrence of stop_token.
If the stream ends without the token, return the full concatenation.
Constraints
len(stop_token) >= 1
Chunk lengths vary.
Examples / Tests
Input: chunks=["hello wor","ldENDxxx"], stop_token="END"
Output: "hello world"
(token split across chunks)
Input: chunks=["ab","c","def"], stop_token="bcd"
Output: "a"
(token at boundary)
Input: chunks=["foo","bar"], stop_token="bar"
Output: "foo"
(token never appears)
Input: chunks=["aa","bb"], stop_token="cc"
Output: "aabb"
(multiple occurrences; take the first)
Input: chunks=["xxSTOPyySTOPzz"], stop_token="STOP"
Output: "xx"
Example
Input
hello wor
ldENDxxx
END
Output
hello world