Count Items Matching A Rule
01 · Problem
You are given an array items where items[i] = [type_i, color_i, name_i] describes one item, plus a rule given by two strings ruleKey and ruleValue.
ruleKey names which field of each item to look at ("type", "color" or "name"); an item matches when that field equals ruleValue exactly.
Comparisons are exact and case-sensitive. Return the number of items that match the rule (0 if none do).
02 · Examples
items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
1
Only the second item has color "silver".
items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
2
The first and third items have type "phone". The second item is named "phone" but its type is "computer", so it does not match.
items = [["tv","black","sony"],["laptop","black","dell"]], ruleKey = "name", ruleValue = "apple"
0
No item is named "apple".
03 · Constraints
- 011 <= items.length <= 104
- 021 <= type_i.length, color_i.length, name_i.length, ruleValue.length <= 10
- 03ruleKey is one of "type", "color" or "name"
- 04All strings consist only of lowercase English letters
04 · Optimal complexity
- Time
- O(n * L)
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.