#1773

Count Items Matching a Rule

Easy List of lists

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Each item is a list [type, color, name]. Given a rule key ("type", "color" or "name") and a value, count items whose matching field equals the value.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: look at one field (type, color or name) of each item and count matches.

Why this works: convert the rule key to a column index once, then each item is a single equals() check.

Example: ruleKey = "color", ruleValue = "silver"

index = 1["phone","blue","pixel"]     → blue ≠ silver["computer","silver","lenovo"] → match["phone","gold","iphone"]    → gold ≠ silveranswer: 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using == on Strings, which compares references and can fail even when the text matches.