#1773
Easy Algorithms Count items matching a rule
Array String
85.3% acceptance
Feb 25, 2026
2035
286
You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item.
Return the number of items that match the given rule.
Solution
Rust
Time O(n)
Space O(1)
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
}
}