##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1773.html

# 1773. Count Items Matching a Rule

Easy

## Description

You are given an array items, where each items[i] = [type_i, color_i, name_i] describes the type, color, and name of the i-th item. You are also given a rule represented by two strings, ruleKey and ruleValue.

The i-th item is said to match the rule if one of the following is true:

• ruleKey == "type" and ruleValue == type*i.
• ruleKey == "color" and ruleValue == color*i.
• ruleKey == "name" and ruleValue == name_i.

Return the number of items that match the given rule.

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^4
• 1 <= type_i.length, color_i.length, name_i.length, ruleValue.length <= 10
• ruleKey is equal to either "type", "color", or "name".
• All strings consist only of lowercase letters.

## Solution

Loop over all items. For each item, check whether it matches ruleKey and ruleValue. Count the items that match the rule and return the count.

• class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int count = 0;
for (List<String> item : items) {
String type = item.get(0), color = item.get(1), name = item.get(2);
if ("type".equals(ruleKey) && type.equals(ruleValue))
count++;
else if ("color".equals(ruleKey) && color.equals(ruleValue))
count++;
else if ("name".equals(ruleKey) && name.equals(ruleValue))
count++;
}
return count;
}
}

############

class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
int ans = 0;
for (var v : items) {
if (v.get(i).equals(ruleValue)) {
++ans;
}
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/count-items-matching-a-rule/
// Time: O(N)
// Space: O(1)
class Solution {
public:
int countMatches(vector<vector<string>>& A, string ruleKey, string ruleValue) {
int ans = 0, i = ruleKey == "type" ? 0 : (ruleKey == "color" ? 1 : 2);
for (auto &a : A) ans += a[i] == ruleValue;
return ans;
}
};

• class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
return sum(v[i] == ruleValue for v in items)

############

# 1773. Count Items Matching a Rule
# https://leetcode.com/problems/count-items-matching-a-rule/

class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
check = 0
if ruleKey == "color": check = 1
elif ruleKey == "name": check = 2

res = 0
for item in items:
if item[check] == ruleValue:
res += 1

return res


• func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
for _, v := range items {
if v[i] == ruleValue {
ans++
}
}
return
}

• function countMatches(
items: string[][],
ruleKey: string,
ruleValue: string,
): number {
const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
}


• impl Solution {
pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
let key = if rule_key == "type" {
0
} else if rule_key == "color" {
1
} else {
2
};
items.iter().filter(|v| v[key] == rule_value).count() as i32
}
}