Count Items Matching a Rule (LeetCode 1773): Solution and Explanation
Each item is a triple [type, color, name]. Given a rule such as color == "silver", count how many items satisfy it.
Intuition
The rule never looks at more than one field. If ruleKey is "color", the type and name of every item are irrelevant, so the table of items collapses to a single column.
The fields always sit in the same order, so the key translates to a fixed position: type is index 0, color is 1, name is 2. Do that translation once, before the loop, instead of branching on the key for every item.
After that the problem is a plain count: walk down the chosen column and add one each time the value equals ruleValue.
Approach: Key to index, then one pass
- Set
idxto 0, 1 or 2 depending on whetherruleKeyis "type", "color" or "name". - Set
count = 0. - For every item, if
item[idx] == ruleValue, increasecount. - Return
count.
Step-by-step example
items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
- ruleKey is "color", so idx = 1 and only the colors matter: blue, silver, gold.
- Item 0 has color "blue". Not a match, count stays 0.
- Item 1 has color "silver". Match, count becomes 1.
- Item 2 has color "gold". Not a match.
- Return 1.
Code: Python, Java, C++
1class Solution:
2 def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
3 idx = {"type": 0, "color": 1, "name": 2}[ruleKey]
4 count = 0
5 for item in items:
6 if item[idx] == ruleValue:
7 count += 1
8 return count1class Solution {
2 public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
3 int idx = ruleKey.equals("type") ? 0 : ruleKey.equals("color") ? 1 : 2;
4 int count = 0;
5 for (List<String> item : items) {
6 if (item.get(idx).equals(ruleValue)) {
7 count++;
8 }
9 }
10 return count;
11 }
12}1class Solution {
2public:
3 int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
4 int idx = ruleKey == "type" ? 0 : ruleKey == "color" ? 1 : 2;
5 int count = 0;
6 for (auto& item : items) {
7 if (item[idx] == ruleValue) {
8 count++;
9 }
10 }
11 return count;
12 }
13};Time and space complexity
O(n)
Each of the n items gets one string comparison, and the strings are at most 10 characters long.
O(1)
Only the index and the counter are stored.
Other approaches
Branch on the key inside the loop (time O(n), space O(1))
Check ruleKey for every item and compare the matching field. It gives the same answer but repeats the same decision n times and is easier to get wrong.
Filter expression (time O(n), space O(1))
Write the count as sum(1 for it in items if it[idx] == ruleValue) or a stream filter. Same work in one line once idx is known.
Common mistakes
- Comparing ruleValue against every field of the item. An item named "phone" does not match the rule type == "phone".
- Mixing up the field order. It is always type, color, name.
- In Java, comparing strings with == instead of equals.
Frequently asked questions
Why build the index before the loop?
The key is the same for every item, so the decision only needs to be made once. Inside the loop you then do a single array lookup and comparison.
Can a hash map help here?
Not for one query. Every item has to be looked at anyway. A map of value counts per field would only pay off if many different rules were asked against the same items.
What if nothing matches?
The counter never increases and the answer is 0.