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 <= 1041 <= typei.length, colori.length, namei.length, ruleValue.length <= 10ruleKey is equal to either "type", "color", or "name".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.
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.
Time Complexity: O(n), where n is the number of items.
Space Complexity: O(1), as we use a constant amount of space.
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.
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.
| Approach | Complexity |
|---|---|
| Index Mapping Method | Time Complexity: O(n), where n is the number of items. |
| Direct Attribute Matching | Time Complexity: O(n), with n being the number of items, as each item must be checked. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Index Mapping Method | O(n) | O(1) | Best general approach when attribute positions are fixed and you want cleaner loop logic |
| Direct Attribute Matching | O(n) | O(1) | Simple implementation when there are only a few attributes and readability is preferred |
1773 Count Items Matching a Rule | Zero to FAANG Kunal | Assignment Solution | Leetcode • Programmers Zone • 5,829 views views
Watch 9 more video solutions →Practice Count Items Matching a Rule with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor