Leet Code — 1773. Count Items Matching a Rule

Ben Pereira - Aug 21 '23 - - Dev Community

It’s an easy problem 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.

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player