Minimum Stores for a Shopping List
Problem statement
You have a shopping list and a collection of stores. Each store carries a set of items. Return the minimum number of distinct stores you must visit so that every item on the shopping list can be purchased.
An item may be purchased from any visited store that carries it. Duplicate names in the shopping list represent the same required item. Return -1 when at least one required item cannot be covered.
Function
minimumStores(stores: String[][], shoppingList: String[]) → intExamples
Example 1
stores = [["milk","bread"],["eggs"],["bread","eggs"]]shoppingList = ["milk","eggs"]return = 2No single store carries both required items, so two visits are necessary.
Example 2
stores = [["apple","tea"],["tea","rice","apple"],["rice"]]shoppingList = ["apple","rice","tea"]return = 1The second store covers the entire list.
Example 3
stores = [["a"],["b"]]shoppingList = ["a","c"]return = -1No store carries c.
Constraints
0 <= stores.length <= 50.0 <= shoppingList.length <= 20.- Every item name is a non-empty case-sensitive ASCII string of length at most
40. - Repeated item names within a store do not change its coverage.