Leet Code — 1773. Count Items Matching a Rule
Tags: leetcode, java, interviewquestions, string
Published: 2023-08-21 20:15:31

It’s an easy [problem](https://leetcode.com/problems/count-items-matching-a-rule) by Leet Code, the description being:

> 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.

> **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.

Through the description you can identify that the ruleKey determines the index of the item inside items array that can be either type, color or name.

The first thing to do is retrieve that related index based on the String (that can be done with if conditions and switch case for example) and after that an iteration and comparison with rule value adding a count when true would be enough in this case.

```java
class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int counter = 0;
        final int ruleKeyIndex = "type".equals(ruleKey) ? 0 : ("color".equals(ruleKey) ? 1 : 2);
        for(int i=0;i<items.size();i++){
            if(items.get(i).get(ruleKeyIndex).equals(ruleValue)){
                counter++;
            }
        }
        return counter;
    }
}
```

In the example it shows the ruleKeyIndex being retrieve by use of multiple ternary operators and then the iteration and adding on counter based on if the ruleValue matches.

> Runtime: 3 ms, faster than 99.92% of Java online submissions for Count Items Matching a Rule.
> Memory Usage: 47.5 MB, less than 36.88% of Java online submissions for Count Items Matching a Rule.

Second example instead of using ternary operators I’ve changed to another method that uses switch-case to get the index needed (note that a break is not needed due to return).

```java
class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int counter = 0;
        final int ruleKeyIndex = getRuleKeyIndex(ruleKey);
        for(int i=0;i<items.size();i++){
            if(items.get(i).get(ruleKeyIndex).equals(ruleValue)){
                counter++;
            }
        }
        return counter;
    }
    
    public int getRuleKeyIndex(final String ruleKey) {
        switch(ruleKey) {
            case "type":
                return 0;
            case "color":
                return 1;
        }
        return 2;
    }
}
```

Under the hood the switch becomes if conditions so the end result performance wise is the same.

> Runtime: 3 ms, faster than 99.92% of Java online submissions for Count Items Matching a Rule.
> Memory Usage: 46.8 MB, less than 97.41% of Java online submissions for Count Items Matching a Rule.

You could also use stream instead of a simple for, less boilerplate, but unfortunately brings the performance down a bit, but nothing substantial.

```java
class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        final int ruleKeyIndex = getRuleKeyIndex(ruleKey);
        return (int) items.stream().filter(i -> i.get(ruleKeyIndex).equals(ruleValue)).count();
    }
    
    public int getRuleKeyIndex(final String ruleKey) {
        switch(ruleKey) {
            case "type":
                return 0;
            case "color":
                return 1;
        }
        return 2;
    }
}
```

> Runtime: 4 ms, faster than 83.02% of Java online submissions for Count Items Matching a Rule.
> Memory Usage: 47.1 MB, less than 79.94% of Java online submissions for Count Items Matching a Rule.

---

That’s it! If there is anything thing else to discuss feel free to drop a comment, until next post! :)