Count items matching a rule

November 2, 2022

array-and-hashmap

Problem URL: Count items matching a rule

We will iterate over the items and check if the rule matches. If it does, we increment the count.

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        d = {'type': 0, 'color': 1, 'name': 2}

        count = 0
        for item in items:
            if item[d[ruleKey]] == ruleValue:
                count += 1

        return count

Time complexity: O(n)
Space complexity: O(1)