Count Items Matching a Rule
EasyUpdated: Aug 2, 2025
Practice on:
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"andruleValue == typei.ruleKey == "color"andruleValue == colori.ruleKey == "name"andruleValue == namei.
Return the number of items that match the given rule.
Examples
Example 1
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
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^41 <= typei.length, colori.length, namei.length, ruleValue.length <= 10ruleKeyis 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
- Map ruleKey to its corresponding index: "type" → 0, "color" → 1, "name" → 2.
- Iterate through each item in items.
- For each item, check if the value at the mapped index equals ruleValue.
- Count and return the number of matches.
Code
C++
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;
}
};
Go
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
}
Java
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;
}
}
Kotlin
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 }
}
}
Python
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)
Rust
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
}
}
TypeScript
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.