Back to blog
Mar 02, 2025
3 min read

Count Items Matching a Rule

Count items in a list that match a given rule based on type, color, or name attributes.

Difficulty: Easy | Acceptance: 85.30% | Paid: No Topics: Array, String

You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a string ruleKey and a string ruleValue.

The ith item is said to match the rule if one of the following is true:

ruleKey == “type” and ruleValue == typei. ruleKey == “color” and ruleValue == colori. ruleKey == “name” and ruleValue == namei.

Return the number of items that match the given rule.

Examples

Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
Output: 1
Explanation: Only the second item matches the rule.
Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
Output: 2
Explanation: The first and third items match the rule.

Constraints

1 <= items.length <= 10⁴
1 <= typei.length, colori.length, namei.length <= 10
ruleKey is equal to "type", "color", or "name".
All strings consist only of lowercase letters.

Linear Scan with Index Mapping

Intuition Map each ruleKey to its corresponding index (type→0, color→1, name→2) and iterate through items counting matches.

Steps

  • Create a mapping from ruleKey to index position
  • Iterate through each item and check if the value at the mapped index equals ruleValue
  • Increment counter for each match and return total
python
from typing import List

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -&gt; int:
        index_map = {"type": 0, "color": 1, "name": 2}
        index = index_map[ruleKey]
        count = 0
        for item in items:
            if item[index] == ruleValue:
                count += 1
        return count

Complexity

  • Time: O(n) where n is the number of items
  • Space: O(1)
  • Notes: Most efficient for single query scenarios

Functional Programming Approach

Intuition Use built-in functional programming constructs like filter, map, and reduce to declaratively count matching items.

Steps

  • Map ruleKey to its corresponding index
  • Apply filter function to keep only matching items
  • Return the count of filtered items
python
from typing import List

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -&gt; int:
        index_map = {"type": 0, "color": 1, "name": 2}
        index = index_map[ruleKey]
        return sum(1 for item in items if item[index] == ruleValue)

Complexity

  • Time: O(n) where n is the number of items
  • Space: O(1) or O(n) depending on language implementation
  • Notes: More readable but may have slight overhead compared to imperative approach

Counter/HashMap Approach

Intuition Build a frequency map of all values at the specified position, then lookup the count for ruleValue.

Steps

  • Map ruleKey to its corresponding index
  • Create a counter/hashmap counting occurrences of each value at that index
  • Return the count for ruleValue from the map
python
from typing import List
from collections import Counter

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -&gt; int:
        index_map = {"type": 0, "color": 1, "name": 2}
        index = index_map[ruleKey]
        counter = Counter(item[index] for item in items)
        return counter.get(ruleValue, 0)

Complexity

  • Time: O(n) where n is the number of items
  • Space: O(k) where k is the number of unique values at the specified position
  • Notes: Useful if multiple queries on same data are expected