1773. Count Items Matching a Rule LeetCode Solution

In this guide, you will get 1773. Count Items Matching a Rule LeetCode Solution with the best time and space complexity. The solution to Count Items Matching a Rule problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Items Matching a Rule solution in C++
  4. Count Items Matching a Rule solution in Java
  5. Count Items Matching a Rule solution in Python
  6. Additional Resources
1773. Count Items Matching a Rule LeetCode Solution image

Problem Statement of 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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1773. Count Items Matching a Rule LeetCode Solution in C++

class Solution {
 public:
  int countMatches(vector<vector<string>>& items, string ruleKey,
                   string ruleValue) {
    const int index = ruleKey == "type" ? 0 : ruleKey == "color" ? 1 : 2;
    return ranges::count_if(items, [index, &ruleValue](const auto& item) {
      return item[index] == ruleValue;
    });
  }
};
/* code provided by PROGIEZ */

1773. Count Items Matching a Rule LeetCode Solution in Java

class Solution {
  public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
    if (ruleKey.equals("type"))
      return count(items, 0, ruleValue);
    if (ruleKey.equals("color"))
      return count(items, 1, ruleValue);
    return count(items, 2, ruleValue);
  }

  private int count(List<List<String>> items, int index, final String ruleValue) {
    return (int) items.stream()
        .map(item -> item.get(index))
        .filter(s -> s.equals(ruleValue))
        .count();
  }
}
// code provided by PROGIEZ

1773. Count Items Matching a Rule LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.