571. Find Pattern in Infinite Stream II
You are provided with a binary array pattern and an object stream of class InfiniteStream that represents an infinite stream of bits indexed from 0.
The class InfiniteStream exposes the following function:
- int next(): Reads and returns a single bit (either 0 or 1) from the stream.
Your task is to return the first starting index where the sequence of bits read from the stream matches the given pattern. For instance, if the pattern is [1, 0], the first occurrence corresponds to the segment highlighted in the stream [0, 1, 0, 1, ...].
Example 1:
Input : stream = [0, 0, 1, 0, 1, 1, ...], pattern = [0, 1]
Output : 1
Explanation : The pattern [0, 1] first appears starting at index 1 in the stream [0, 0, 1, 0, 1, 1, ...].
Example 2:
Input : stream = [1, 1, 1, 1, ...], pattern = [1, 1]
Output : 0
Explanation : The pattern [1, 1] is immediately found at the very beginning of the stream [1, 1, 1, 1, ...], starting at index 0.
Now Your Turn!
Pick the correct output for the given inputInput : stream = [1, 0, 1, 0, 0, 1, ...], pattern = [1, 0, 0]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= pattern.length <= 100
- pattern consists only of 0 and 1.
- The stream produces only 0 and 1.
- The input is designed such that the starting index of the pattern appears within the first 10⁵ bits of the stream.