Problem

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 rule represented by two strings, ruleKey and 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

Example 1

1
2
3
4
5
6
7

    
    
    Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
    Output: 1
    Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].
    

Example 2

1
2
3
4
5
6

    
    
    Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
    Output: 2
    Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.

Constraints

  • 1 <= items.length <= 10^4
  • 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.

Solution

Method 1 – Simple Iteration and Matching

Intuition

We just need to check each item and see if the value at the index corresponding to the ruleKey matches the ruleValue.

Approach

  1. Map ruleKey to its corresponding index: “type” → 0, “color” → 1, “name” → 2.
  2. Iterate through each item in items.
  3. For each item, check if the value at the mapped index equals ruleValue.
  4. Count and return the number of matches.

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
    int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
        int idx = ruleKey == "type" ? 0 : ruleKey == "color" ? 1 : 2;
        int ans = 0;
        for (auto& item : items) if (item[idx] == ruleValue) ans++;
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func countMatches(items [][]string, ruleKey string, ruleValue string) int {
    idx := map[string]int{"type": 0, "color": 1, "name": 2}[ruleKey]
    ans := 0
    for _, item := range items {
        if item[idx] == ruleValue {
            ans++
        }
    }
    return ans
}
1
2
3
4
5
6
7
8
class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int idx = ruleKey.equals("type") ? 0 : ruleKey.equals("color") ? 1 : 2;
        int ans = 0;
        for (List<String> item : items) if (item.get(idx).equals(ruleValue)) ans++;
        return ans;
    }
}
1
2
3
4
5
6
class Solution {
    fun countMatches(items: List<List<String>>, ruleKey: String, ruleValue: String): Int {
        val idx = when(ruleKey) { "type" -> 0; "color" -> 1; else -> 2 }
        return items.count { it[idx] == ruleValue }
    }
}
1
2
3
4
class Solution:
    def countMatches(self, items: list[list[str]], ruleKey: str, ruleValue: str) -> int:
        idx = {"type": 0, "color": 1, "name": 2}[ruleKey]
        return sum(1 for item in items if item[idx] == ruleValue)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
impl Solution {
    pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
        let idx = match rule_key.as_str() {
            "type" => 0,
            "color" => 1,
            _ => 2,
        };
        items.iter().filter(|item| item[idx] == rule_value).count() as i32
    }
}
1
2
3
4
5
6
class Solution {
    countMatches(items: string[][], ruleKey: string, ruleValue: string): number {
        const idx = ruleKey === "type" ? 0 : ruleKey === "color" ? 1 : 2;
        return items.filter(item => item[idx] === ruleValue).length;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the number of items, as we check each item once.
  • 🧺 Space complexity: O(1), only a constant amount of space is used.