Count Items Matching a Rule - Problem

Imagine you're working as a digital inventory manager for an online store! ๐Ÿ›๏ธ

You have a collection of items in your inventory, where each item is described by three properties: [type, color, name]. Your task is to count how many items match a specific filtering rule.

A filtering rule consists of two parts:

  • ruleKey: Which property to filter by ("type", "color", or "name")
  • ruleValue: The exact value that property should have

An item matches the rule if the specified property equals the target value. For example, if ruleKey = "color" and ruleValue = "red", then only red items will match.

Goal: Return the total count of items that satisfy the given filtering rule.

Example: If you have items like [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]] and rule ruleKey="type", ruleValue="phone", the answer is 2 because two items are phones.

Input & Output

example_1.py โ€” Basic Type Matching
$ Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]] ruleKey = "type" ruleValue = "phone"
โ€บ Output: 2
๐Ÿ’ก Note: We're looking for items with type="phone". The first item ["phone","blue","pixel"] matches, the second ["computer","silver","lenovo"] doesn't match, and the third ["phone","gold","iphone"] matches. Total: 2 matches.
example_2.py โ€” Color Matching
$ Input: items = [["phone","blue","pixel"],["computer","silver","mac"],["phone","gold","iphone"]] ruleKey = "color" ruleValue = "silver"
โ€บ Output: 1
๐Ÿ’ก Note: We're searching for items with color="silver". Only the second item ["computer","silver","mac"] has a silver color, so the count is 1.
example_3.py โ€” No Matches Edge Case
$ Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]] ruleKey = "name" ruleValue = "samsung"
โ€บ Output: 0
๐Ÿ’ก Note: We're looking for items with name="samsung", but none of the items have this name. The names are "pixel", "lenovo", and "iphone", so the count is 0.

Constraints

  • 1 โ‰ค items.length โ‰ค 104
  • 1 โ‰ค typei.length, colori.length, namei.length, ruleValue.length โ‰ค 10
  • ruleKey is equal to either "type", "color", or "name"
  • All strings consist only of lowercase letters

Visualization

Tap to expand
Digital Store Inventory FilterInventory Conveyor Beltphone|blue|pixelpc|silver|macphone|gold|iphonetablet|black|ipadSCANNERFilter: typeValue: phoneโœ“Match 1โœ—No matchโœ“Match 2โœ—No matchRESULTCount: 2
Understanding the Visualization
1
Setup Filter
Configure the scanner to look for a specific property (type, color, or name)
2
Scan Inventory
Pass each item through the scanner one by one
3
Check & Count
For each item, check if the target property matches the filter value
4
Final Count
Return the total number of items that passed the filter
Key Takeaway
๐ŸŽฏ Key Insight: This is a straightforward filtering problem that requires just one pass through the data. No complex algorithms needed - just map the rule key to the correct array position and count matches!
Asked in
Amazon 25 Apple 18 Google 15 Microsoft 12
42.0K Views
Medium Frequency
~8 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen