Forem

Cover image for 1773. Count Items Matching a Rule.
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on

1773. Count Items Matching a Rule.

Introduction

Image description

Here our problem says, there are one 2D list of items and one ruleKey and ruleValue. We have to traverse all the element and first match with the ruleKey value. If ruleKey is "type" then we will check the ruleValue with the first elements of the list.

Examples

Image description

Hint

Image description

Steps

  1. Take counter variable
  2. Run a for each loop
  3. Check if ruleKey value == type, and ruleValue == item(0)
  4. Or ruleKey == color, and ruleValue == item(1)
  5. or ruleKey == name, and ruleValue == item(2)
  6. Now if any of these are true then increase the count.
  7. return the count.

JavaCode

import java.util.List;

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

Output

Image description

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay