Skip to main content

Count Items Matching a Rule - Solution & Explanation

EasyArrayString13 min readAsked at: Meta, Google
Practice this problem

Problem Statement

You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.

The ith item is said to match the rule if one of the following is true:

  • ruleKey == "type" and ruleValue == typei.
  • ruleKey == "color" and ruleValue == colori.
  • ruleKey == "name" and ruleValue == namei.

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 <= 104
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey is equal to either "type", "color", or "name".
  • All strings consist only of lowercase letters.

Approach Overview

Problem Overview: You receive a list of items where each item contains three attributes: type, color, and name. Given a ruleKey and ruleValue, count how many items have the matching attribute value.

Approach 1: Index Mapping Method (O(n) time, O(1) space)

Each item is stored as an array of three strings. The position of each attribute is fixed: type → 0, color → 1, and name → 2. Map the incoming ruleKey to its corresponding index, then iterate through the items and compare items[i][index] with ruleValue. Increment a counter whenever they match. The key insight is converting the string key into a constant index so each check becomes a direct array lookup.

This approach performs a single pass through the input, making the time complexity O(n) where n is the number of items. No additional data structures are required, so space complexity remains O(1). It works well when the attribute positions are fixed and known in advance.

Approach 2: Direct Attribute Matching (O(n) time, O(1) space)

Instead of mapping indices, evaluate the rule condition directly during iteration. For each item, check the relevant attribute using conditional logic: compare item[0] when ruleKey == "type", item[1] for "color", and item[2] for "name". Increment the count when the chosen attribute equals ruleValue. This keeps the logic explicit and easy to read.

The algorithm still scans the array once, giving O(n) time complexity with O(1) extra space. Because there are only three possible attributes, the conditional checks are constant time and do not affect scalability.

Both methods rely on straightforward iteration over a array and comparison of string values. No advanced data structures are needed, which is why the problem is categorized as Easy.

Recommended for interviews: The Index Mapping Method is typically preferred. It separates rule interpretation from the main loop and keeps the iteration logic clean. Interviewers expect a linear scan with constant extra space. Showing the direct conditional approach first demonstrates understanding, but the index mapping version looks cleaner and scales better if more attributes are added.

Approach 1: Index Mapping Method

In this approach, we map the ruleKey to its corresponding index in the item list: 'type' maps to index 0, 'color' maps to index 1, and 'name' maps to index 2. We iterate through the items and count how many of them match the ruleValue at the mapped index.

The function countMatches takes the items array, its size, the ruleKey, and the ruleValue. It determines the index for comparison based on the ruleKey, and iterates through each item in items, counting how many times the item's attribute matches the ruleValue.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of items.
Space Complexity: O(1), as we use a constant amount of space.

Try this approach in the editor →

Approach 2: Direct Attribute Matching

This approach directly checks each item attribute against the ruleValue based on the ruleKey. We manage this by case-selecting the property to compare without indexing.

This solution performs a straightforward comparison using string comparison functions for every item. Depending on the ruleKey, it checks if the respective part of the item equals the ruleValue.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), with n being the number of items, as each item must be checked.
Space Complexity: O(1), given the constant space requirements.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Index Mapping Method

Time Complexity: O(n), where n is the number of items.
Space Complexity: O(1), as we use a constant amount of space.

Direct Attribute Matching

Time Complexity: O(n), with n being the number of items, as each item must be checked.
Space Complexity: O(1), given the constant space requirements.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Index Mapping MethodO(n)O(1)Best general approach when attribute positions are fixed and you want cleaner loop logic
Direct Attribute MatchingO(n)O(1)Simple implementation when there are only a few attributes and readability is preferred

Video Solution

Count items matching a rule leetcode solution in java in Hindi (Leetcode 1773)Engineering Digest2,214 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Items Matching a Rule easy or hard?
Count Items Matching a Rule is classified as Easy on LeetCode with a high acceptance rate above 80%. The problem mainly tests basic array traversal, string comparison, and conditional logic.
Count Items Matching a Rule Python/Java solution
Both Python and Java implementations follow the same idea: determine the index for the ruleKey, iterate through the items list, and count matches. The algorithm uses a simple loop and string equality check, giving O(n) time and O(1) space complexity.
How to solve Count Items Matching a Rule in O(n)?
Map the ruleKey to its attribute index: type → 0, color → 1, name → 2. Iterate through each item and check if items[i][index] equals ruleValue. Increment a counter whenever the condition is true. The single pass over the array ensures O(n) time complexity.
What is the best approach for Count Items Matching a Rule?
The index mapping method is the cleanest approach. Convert the ruleKey (type, color, or name) into its corresponding index in the item array, then iterate once through the list and compare values. This runs in O(n) time with O(1) extra space and keeps the loop logic simple.
Is Count Items Matching a Rule asked at Google/Amazon/Meta?
This problem is an entry-level array and string filtering question commonly used in screening rounds or practice sets. Variants of attribute filtering appear in interviews at companies like Amazon and Google to test iteration, indexing, and conditional logic.
What data structure is used in Count Items Matching a Rule?
The core data structure is a 2D array (or list of lists) containing string attributes. The solution mainly uses array traversal and string comparison. No additional structures like hash maps are required for the optimal approach.
What is the time complexity of Count Items Matching a Rule?
The optimal solution runs in O(n) time because the algorithm scans the list of items exactly once. Each comparison is constant time. Space complexity is O(1) since only a counter and a few variables are used.

Ready to solve this problem?

Practice Count Items Matching a Rule with our built-in code editor and test cases.

Practice on FleetCode