468. Department Visits Saved for a Shopping List
Department Visits Saved for a Shopping List

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.

Method Signature

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.
  • Returns the number of department visits saved.

Rules

  • Buying in list order, the first item starts a visit, and every later item starts a new visit when its department differs from the department of the item just before it.
  • Buying one department at a time, each department that appears on the list is visited exactly once.
  • Visits saved = visits in list order − visits one department at a time.

Constraints

  • 1 ≤ products.size() ≤ 100,000
  • 1 ≤ shoppingList.size() ≤ products.size()
  • Product and department names are 1 to 20 characters long, contain only English letters and spaces, and do not start or end with a space.
  • Product and department names are case-sensitive.
  • All product names in products are distinct.
  • Every item in shoppingList is a product name from products, and no item appears twice.

Examples

Example 1

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

  • In list order: Dairy (Eggs) → Pantry (Rice) → Dairy (Cheese) → Pantry (Beans) → Produce (Apples) → Meat (Bacon) → Produce (Onions) = 7 visits.
  • One department at a time: Dairy (Eggs, Cheese) → Pantry (Rice, Beans) → Produce (Apples, Onions) → Meat (Bacon) = 4 visits.
  • Visits saved: 7 − 4 = 3.

Example 2

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

  • In list order: Drinks (Juice, Soda) → Snacks (Cookies, Chips) → Frozen Foods (Ice Cream) = 3 visits.
  • One department at a time also takes 3 visits, so no visits are saved.

Example 3

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

  • In list order: Bakery (Hot Dog Buns) → Deli (Sliced Ham, Turkey) → Pantry (Peanut Butter) → Bakery (Muffins) = 4 visits.
  • One department at a time: Bakery (Hot Dog Buns, Muffins) → Deli (Sliced Ham, Turkey) → Pantry (Peanut Butter) = 3 visits.
  • Visits saved: 4 − 3 = 1.


Please use Laptop/Desktop or any other large screen to add/edit code.