Ctrl K

Catalog Filter

Easy
In an online store catalog every product is described by three strings: products[i] = [type, color, name]. A shopper turns on a filter: the field field (one of "type", "color" or "name") must equal the string value. A product passes the filter when its value in that field matches value exactly. Return the number of products that pass the filter.
Example 1
products=[['mug', 'white', 'classic'], ['lamp', 'green', 'nordic'], ['mug', 'green', 'tall']], field="color", value="green" → 2

The lamp and the tall mug are green, so 2 products match.

Example 2
products=[['sofa', 'grey', 'corner'], ['chair', 'grey', 'sofa']], field="type", value="sofa" → 1

Only the first product has type "sofa". For the second one "sofa" is the name, but the filter checks the type.

Example 3
products=[['desk', 'oak', 'loft']], field="name", value="studio" → 0

No product is named "studio", so the answer is 0.

Constraints
  • 1 ≤ products.length ≤ 10⁴
  • products[i].length = 3
  • Every string in products[i], and value, has length from 1 to 30
  • field is "type", "color" or "name"
  • All strings other than field consist of lowercase English letters, digits, spaces and the characters -, _, &
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
products=[['mug', 'white', 'classic'], ['lamp', 'green', 'nordic'], ['mug', 'green', 'tall']], field="color", value="green"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.