[99클럽 코테 스터디 29일차 TIL <문자열>]

서원준·2024년 6월 17일


29일차 - Count Items Matching a Rule
성공코드

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        # Define the index based on ruleKey
        if ruleKey == "type":
            index = 0
        elif ruleKey == "color":
            index = 1
        elif ruleKey == "name":
            index = 2
        
        count = 0
        for item in items:
            if item[index] == ruleValue:
                count += 1
        
        return count

오늘은 문자열에 관한 알고리즘 문제다.
문제를 보면
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.
타입,색상,이름으로 된 아이템들이 주어지게 되는데
rulekey 에 해당하는 rulevalue를 가진 아이템이 몇개인지 리턴하는 문제이다.
그래서 각각 해당하는 index를 지정하고 rulekey,value 에 부합하는 아이템이 뭔지
계산해서 카운팅하는 식으로 문제를 해결했다.

profile
상상은 꿈을 현실로 만들어준다

0개의 댓글