본문 바로가기

IT/코딩테스트

[Leetcode] 2103. Rings and Rods

728x90

https://leetcode.com/problems/rings-and-rods/

 

Rings and Rods - LeetCode

Can you solve this real interview question? Rings and Rods - There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9. You are given a string rings of length 2n that describes the n rings

leetcode.com

class Solution:
    def countPoints(self, rings: str) -> int:
        rods = {    # ["R", "G", "B"]
            "0":[False, False, False], 
            "1":[False, False, False], 
            "2":[False, False, False], 
            "3":[False, False, False], 
            "4":[False, False, False], 
            "5":[False, False, False],
            "6":[False, False, False],
            "7":[False, False, False],
            "8":[False, False, False],
            "9":[False, False, False]
        }

        ring_list = [rings[i:i+2] for i in range(0, len(rings), 2)]

        for ring in ring_list:
            if ring[0] == "R":
                rods[ring[1]][0] = True
            elif ring[0] == "G":
                rods[ring[1]][1] = True
            elif ring[0] == "B":
                rods[ring[1]][2] = True

        count = 0
        for key, value in rods.items():
            if value == [True, True, True]:
                count += 1

        return count
728x90