You are given:
-
An
ordered
ingredient list
ingredients[0..n-1]
.
-
A list of
m
recipes. Each recipe
recipes[j]
is itself a list of ingredients and is intended to represent a sequence of
consecutive
ingredients.
Task
For each recipe, determine whether it can be formed by taking a contiguous subarray of ingredients (i.e., there exists an index s such that ingredients[s .. s + len(recipe)-1] equals the recipe exactly, element by element).
Return an array ans[0..m-1] of booleans.
Input
-
Array
ingredients
(length
n
)
-
List of arrays
recipes
(total
m
recipes)
Output
-
Boolean array
ans
, where
ans[j] = true
if
recipes[j]
appears contiguously in
ingredients
, else
false
.
Follow-ups
-
How would you solve it if you are allowed only
O(1)
extra space (beyond the input/output)?
-
How would you solve it if
ingredients
arrives as a
data stream
and is too large to store fully?
Constraints (typical)
-
1 <= n <= 2e5
-
1 <= m <= 2e5
-
Total length of all recipes
<= 2e5
-
Ingredients can be treated as strings or integers.