{x}
blog image

Count Items Matching a Rule

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.

Solution Explanation

This problem asks to count the number of items in a list that match a given rule. Each item is a list of strings representing [type, color, name], and the rule is specified by ruleKey (either "type", "color", or "name") and ruleValue. The solution efficiently determines the index corresponding to the ruleKey and then iterates through the items, checking if the value at that index matches ruleValue.

Approach

The core idea is to avoid nested loops or multiple if-else blocks which would increase the time complexity. We use a simple mapping to determine the correct index within each item based on the ruleKey:

  • If ruleKey is "type", the index is 0.
  • If ruleKey is "color", the index is 1.
  • If ruleKey is "name", the index is 2.

This mapping is achieved efficiently using a ternary operator or a similar conditional expression in most programming languages. Once the index is determined, we iterate through the items list and increment a counter whenever the item at the determined index matches ruleValue.

Code Explanation (Python)

The Python solution leverages Python's concise syntax for this task:

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
        return sum(v[i] == ruleValue for v in items)
  • i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2): This line efficiently determines the index i based on the first letter of ruleKey. It's a chained conditional expression.
  • return sum(v[i] == ruleValue for v in items): This line uses a generator expression to efficiently check if each item at index i matches ruleValue and then sums the boolean results (True is 1, False is 0).

Time and Space Complexity Analysis

Time Complexity: O(N), where N is the number of items. We iterate through the list of items only once. The index determination and comparison within the loop are constant-time operations.

Space Complexity: O(1). The space used is constant, regardless of the input size. We only store a few variables (index i and the counter implicitly used by the sum() function). The generator expression doesn't create a large intermediate list in Python 3.

The solutions in other languages follow a similar approach, adapting the syntax and data structures to the language's specifics while maintaining the same core algorithm and efficiency. The C++ solution, for example, uses count_if from the <algorithm> header for a concise solution. The Java solution employs a more explicit loop. All the solutions aim for the optimal O(N) time complexity and O(1) space complexity.