Fresh Promotion Checker
MediumArrays · Amazon
Amazon Fresh runs a promotion described by code_list, an ordered list of groups, where each group is a list of fruit names. A customer's cart is the ordered list of items they bought.
The customer wins if every group can be found in cart as a contiguous run of items such that:
- the groups appear in the same order as in
code_list, - the runs do not overlap (each cart item is used by at most one group),
- inside a group, the special name
"anything"matches any single cart item, and every other name must match exactly.
Any number of other items may appear before, between, or after the matched groups. Return 1 if the customer wins and 0 otherwise. An empty code_list always wins.
Example 1
- Input:
- code_list = [['kiwi', 'anything'], ['mango', 'mango']], cart = ['mango', 'kiwi', 'pear', 'mango', 'lime', 'mango', 'mango']
- Output:
- 1
- Explanation
- The first group matches
kiwi, pearat positions 1-2 (anythingcovers the pear). The mango at position 3 is followed by lime, so the second group cannot start there; it matches at positions 5-6 instead.
Example 2
- Input:
- code_list = [['plum', 'plum'], ['fig']], cart = ['plum', 'fig', 'plum', 'plum']
- Output:
- 0
- Explanation
- The only
plum, plumrun is at the end of the cart, and the fig group must come after it. The fig at position 1 is too early, so the customer does not win.
Example 3
- Input:
- code_list = [['apple'], ['apple']], cart = ['apple']
- Output:
- 0
- Explanation
- Each group needs its own apple, since matched runs cannot overlap, and the cart has only one.
Constraints
0 <= code_list.length <= 1001 <= code_list[i].length <= 1000 <= cart.length <= 1000- Names are lowercase strings;
"anything"only appears incode_list