LeetCode 1773: Count Items Matching a Rule Solution

Master LeetCode problem 1773 (Count Items Matching a Rule), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1773. Count Items Matching a Rule

Problem Explanation

Explanation:

To solve this problem, we need to iterate through the given items array and count the number of items that match the given rule based on the ruleKey and ruleValue. We can simply compare the ruleKey and ruleValue with the corresponding values in each item to determine if it matches the rule.

  • Initialize a counter to keep track of the number of items that match the rule.
  • Iterate through each item in the items array.
  • For each item, check if the ruleKey matches "type", "color", or "name" and compare the ruleValue with the corresponding value in the item.
  • If a match is found, increment the counter.
  • Finally, return the counter as the result.

Time Complexity:

The time complexity of this solution is O(n), where n is the number of items in the given array.

Space Complexity:

The space complexity is O(1) as we are using a constant amount of extra space.

:

Solution Code

class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int count = 0;
        for (List<String> item : items) {
            if ((ruleKey.equals("type") && ruleValue.equals(item.get(0))) ||
                (ruleKey.equals("color") && ruleValue.equals(item.get(1))) ||
                (ruleKey.equals("name") && ruleValue.equals(item.get(2)))) {
                count++;
            }
        }
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1773 (Count Items Matching a Rule)?

This page provides optimized solutions for LeetCode problem 1773 (Count Items Matching a Rule) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1773 (Count Items Matching a Rule)?

The time complexity for LeetCode 1773 (Count Items Matching a Rule) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1773 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1773 in Java, C++, or Python.

Back to LeetCode Solutions