Count Items Matching a Rule

You are given an array items where items[i] = [type_i, color_i, name_i] describes the i-th item, plus a rule given by two strings ruleKey and ruleValue. An item matches the rule when the field named by ruleKey ("type", "color" or "name") equals ruleValue. Return how many items match.

30:00

Count Items Matching a Rule
easy
Free written solution
Topics
Companies

You are given an array items where items[i] = [type_i, color_i, name_i] describes the i-th item, plus a rule given by two strings ruleKey and ruleValue. An item matches the rule when the field named by ruleKey ("type", "color" or "name") equals ruleValue. Return how many items match.

Example 1:
Input: {"items":[["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]],"ruleKey":"color","ruleValue":"silver"}
Output: 1
Constraints:
  • 1≤items.length≤1041 \leq \text{items.length} \leq 10^4

  • 1≤typei.length,colori.length,namei.length,ruleValue.length≤101 \leq \text{type}_i.\text{length}, \text{color}_i.\text{length}, \text{name}_i.\text{length}, \text{ruleValue.length} \leq 10

  • ruleKey\text{ruleKey} is "type", "color" or "name".

  • All strings consist only of lowercase letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"items":[["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]],"ruleKey":"color","ruleValue":"silver"}

The rule is color == "silver". Only one field of each item matters.

rule

color=="silver"

count

0

items

type

color

name

0

phone
blue
pixel

·

1

computer
silver
lenovo

·

2

phone
gold
iphone

·

Variables
VariableValue
ruleKeycolor
ruleValuesilver
DepthFunction Call
Stack empty
0/6