Count Items Matching a Rule (LeetCode 1773): Solution and Explanation

Easy
Array
String

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

  1. Set idx to 0, 1 or 2 depending on whether ruleKey is "type", "color" or "name".
  2. Set count = 0.
  3. For every item, if item[idx] == ruleValue, increase count.
  4. Return count.

Step-by-step example

items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
  1. ruleKey is "color", so idx = 1 and only the colors matter: blue, silver, gold.
  2. Item 0 has color "blue". Not a match, count stays 0.
  3. Item 1 has color "silver". Match, count becomes 1.
  4. Item 2 has color "gold". Not a match.
  5. 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 count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each of the n items gets one string comparison, and the strings are at most 10 characters long.

Space

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.