Calculate Department Visit Difference for Shopping Lists

Asked at Karat · Coding Questions · Difficulty: Easy · Last asked Jul 15, 2026

Given a `String[][] products` representing each product and its corresponding department, such as `["Blueberries", "Produce"]` and `["Milk", "Dairy"]`. There is also a `String[] list` representing a shopping list. There are two ways to shop: one is to buy in order, which requires visiting `m` different departments; the other is to combine the same departments, only needing to visit `n` different departments. Calculate the difference between `m` and `n`.

Loading the problem…