Difficulty: 🟢 Easy
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"
andruleValue == typei
.ruleKey == "color"
andruleValue == colori
.ruleKey == "name"
andruleValue == 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.
1 <= items.length <= 104
1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
ruleKey
is equal to either"type"
,"color"
, or"name"
.- All strings consist only of lowercase letters.
class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count = 0
for type_, color, name in items:
if ruleKey == "type" and type_ == ruleValue:
count += 1
elif ruleKey == "color" and color == ruleValue:
count += 1
elif ruleKey == "name" and name == ruleValue:
count += 1
return count
The given solution counts the number of items that match the given rule by iterating through the items
array and comparing the values of type
, color
, and name
with the ruleValue
based on the ruleKey
.
The algorithm works as follows:
- Initialize a count variable to 0.
- Iterate through each item in the
items
array. - For each item, compare the value of
ruleKey
with the corresponding value (type
,color
, orname
) of the item:- If
ruleKey
is "type" and the value matchesruleValue
, increment the count by 1. - If
ruleKey
is "color" and the value matchesruleValue
, increment the count by 1. - If
ruleKey
is "name" and the value matchesruleValue
, increment the count by 1.
- If
- Return the count, which represents the number of items that match the given rule.
The algorithm compares the values of type
, color
, and name
with ruleValue
based on the value of ruleKey
and increments the count if there is a match.
The time complexity of this algorithm is O(n), where n is the length of the items
array. The algorithm iterates through each item in the items
array once and performs constant-time operations at each iteration.
The space complexity of the algorithm is O(1) as it uses only a constant amount of extra space to store the count variable.
The given solution counts the number of items that match the given rule by comparing the values of type
, color
, and name
with ruleValue
based on ruleKey
. It has a time complexity of O(n) and a space complexity of O(1), making it an efficient solution for the problem at hand.
NB: If you want to get community points please suggest solutions in other languages as merge requests.