39. Count Items Matching a Rule

EasyArrayArray

You are given an array items where each items[i] = [type_i, color_i, name_i]. You are also given a rule represented by two strings ruleKey and ruleValue. Return the number of items that match the given rule.

- ruleKey = "type" matches items[i][0]
- ruleKey = "color" matches items[i][1]
- ruleKey = "name" matches items[i][2]

Input: A 2D string array items, a string ruleKey, and a string ruleValue.

Output: Integer — count of items matching the rule.

Examples

Example 1
Input: [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]],"color","silver"
Output: 1
Explanation: Only item 1 has color='silver'. Return 1.
Example 2
Input: [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]],"type","phone"
Output: 2
Explanation: Items 0 and 2 have type='phone'. Return 2.
Example 3
Input: [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]],"name","pixel"
Output: 1
Explanation: Only item 0 has name='pixel'. Return 1.

Constraints

Asked by

MetaGoogle
Solve this problem in the editor →