Search⌘ K
AI Features

Problem: Count Items Matching a Rule

Understand how to implement a linear search in C++ to count items in an array that match a given rule. Learn to map rule keys to array indices and efficiently compare attribute values. This lesson equips you to solve matching problems by iterating through data with optimal O(n) time and constant space complexity.

Statement

You are given an array items, where each items[i] = [type_i, color_i, name_i] represents the type, color, and name of the ithi^{th} item. You are also given two strings, ruleKey and ruleValue, that together define a matching rule.

An item is considered a match if any of the following conditions holds:

  • ruleKey == "type" and ruleValue == type_i

  • ruleKey == "color" and ruleValue == color_i

  • ruleKey == "name" and ruleValue == name_i

Return the total number of items in items that match the given rule.

Constraints:

  • 11 \leq items.length 104\leq 10^4 ...