Skip to main content

Command Palette

Search for a command to run...

Count Items Matching a Rule

Published
2 min read
Count Items Matching a Rule
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an array items, where each items[i] = [type<sub>i</sub>, color<sub>i</sub>, name<sub>i</sub>] describes the type, color, and name of the i<sup>th</sup> item. You are also given a rule represented by two strings, ruleKey and ruleValue.

The i<sup>th</sup> item is said to match the rule if one of the following is true:

  • ruleKey == "type" and ruleValue == type<sub>i</sub>.

  • ruleKey == "color" and ruleValue == color<sub>i</sub>.

  • ruleKey == "name" and ruleValue == name<sub>i</sub>.

Return the number of items that match the given rule.

LeetCode Problem - 1773

class Solution {
    // Method to count the number of items matching a given rule
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        // Counter to keep track of matching items
        int count = 0;
        // Iterating through each item in the list
        for (List<String> currentItems : items) {
            // Checking if the current item matches the given rule
            if (givenRuleMatchingOrNot(currentItems, ruleKey, ruleValue)) {
                // Incrementing count if the rule matches
                count++;
            }
        }
        // Returning the count of matching items
        return count;
    }

    // Method to check if a given rule matches an item
    public boolean givenRuleMatchingOrNot(List<String> item, String ruleKey, String ruleValue) {
        // Switch statement to handle different rule keys
        return switch (ruleKey) {
            // Checking if the rule value matches the type of the item
            case "type" -> ruleValue.equals(item.get(0));
            // Checking if the rule value matches the color of the item
            case "color" -> ruleValue.equals(item.get(1));
            // Checking if the rule value matches the name of the item
            case "name" -> ruleValue.equals(item.get(2));
            // Default case if the rule key is not recognized
            default -> false;
        };
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.