Before a camping trip, you need to buy groceries from a store where every product belongs to exactly one department. Instead of buying the items on your shopping list in the given order, you plan to buy everything you need from one department before moving to the next.
Return the number of department visits saved by this plan.
int departmentVisitsSaved(List<String> products, List<String> shoppingList)
products: each entry is "productName,department", for example "Milk,Dairy".shoppingList: product names in the order they appear on the list.1 ≤ products.size() ≤ 100,0001 ≤ shoppingList.size() ≤ products.size()products are distinct.shoppingList is a product name from products, and no item appears twice.departmentVisitsSaved( products = List.of("Eggs,Dairy", "Rice,Pantry", "Cheese,Dairy", "Beans,Pantry", "Apples,Produce", "Bacon,Meat", "Onions,Produce", "Milk,Dairy"), shoppingList = List.of("Eggs", "Rice", "Cheese", "Beans", "Apples", "Bacon", "Onions") )
Output: 3
departmentVisitsSaved( products = List.of("Chips,Snacks", "Soda,Drinks", "Ice Cream,Frozen Foods", "Juice,Drinks", "Cookies,Snacks"), shoppingList = List.of("Juice", "Soda", "Cookies", "Chips", "Ice Cream") )
Output: 0
departmentVisitsSaved( products = List.of("Hot Dog Buns,Bakery", "Muffins,Bakery", "Sliced Ham,Deli", "Turkey,Deli", "Peanut Butter,Pantry"), shoppingList = List.of("Hot Dog Buns", "Sliced Ham", "Turkey", "Peanut Butter", "Muffins") )
Output: 1