Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.
The ith item is said to match the rule if one of the following is true:
ruleKey == "type" and ruleValue == typei.ruleKey == "color" and ruleValue == colori.ruleKey == "name" and ruleValue == namei.Return the number of items that match the given rule.
Example 1:
Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver" Output: 1 Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].
Example 2:
Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone" Output: 2 Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.
Constraints:
1 <= items.length <= 1041 <= typei.length, colori.length, namei.length, ruleValue.length <= 10ruleKey is equal to either "type", "color", or "name".Problem summary: You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue. The ith item is said to match the rule if one of the following is true: ruleKey == "type" and ruleValue == typei. ruleKey == "color" and ruleValue == colori. ruleKey == "name" and ruleValue == namei. Return the number of items that match the given rule.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]] "color" "silver"
[["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]] "type" "phone"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1773: Count Items Matching a Rule
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
int ans = 0;
for (var v : items) {
if (v.get(i).equals(ruleValue)) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1773: Count Items Matching a Rule
func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
for _, v := range items {
if v[i] == ruleValue {
ans++
}
}
return
}
# Accepted solution for LeetCode #1773: Count Items Matching a Rule
class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
return sum(v[i] == ruleValue for v in items)
// Accepted solution for LeetCode #1773: Count Items Matching a Rule
impl Solution {
pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
let key = if rule_key == "type" {
0
} else if rule_key == "color" {
1
} else {
2
};
items.iter().filter(|v| v[key] == rule_value).count() as i32
}
}
// Accepted solution for LeetCode #1773: Count Items Matching a Rule
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {
const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.