[Leet Code] Count Items Matching a Rule

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"].
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.
class Solution(object):
def countMatches(self, items, ruleKey, ruleValue):
"""
:type items: List[List[str]]
:type ruleKey: str
:type ruleValue: str
:rtype: int
"""

dictionary = {"type": 0, "color": 1, "name" : 2}
indexPos = dictionary[ruleKey]
answer = 0

for i,v in enumerate(items):
if v[indexPos] == ruleValue:
answer += 1

return answer

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

👨🏼‍💻Guide for showing Huawei Ads with Applovin Mediation

#IPHONE 13 MINI vs IPHONE 13 PRO MAX além do TAMANHO! #iphone

#IPHONE 13 MINI vs IPHONE 13 PRO MAX além do TAMANHO! #iphone

Our World Partners With The HoloWeb

Overview of next-generation Java frameworks

A Guide To Achieve Clean Code And Best Coding Practices

gOOD  — Bigscal Technologies

Image by moments.com

Why do we forget +c?

Operator In Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

Leetcode — 1. Two Sum

What is Big O Notation? And why do I need to know it to get a Software Engineer job?

Find unique Characters in a String

Merge Sorted Array🐧