[LeetCode]#2103. Rings and Rods

Fatboy Slim
2 min readDec 13, 2021

Environment: Python 3.8

Key technique: set, if, for

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 that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:

  • The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B').
  • The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').

For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.

Return the number of rods that have all three colors of rings on them.

Example 1:

Input: rings = "B0B6G0R6R0R6G9"
Output: 1
Explanation:
- The rod labeled 0 holds 3 rings with all colors: red, green, and blue.
- The rod labeled 6 holds 3 rings, but it only has red and blue.
- The rod labeled 9 holds only a green ring.
Thus, the number of rods with all three colors is 1.

Analysis:

  1. Use set array
  2. Search each location rod its R, G, B rings as below.
  3. Return each set length =>3.

Solution:

class Solution:
def countPoints(self, rings: str) -> int:
rods = [set() for _ in range(10)]
ans=0
for i in range(0, len(rings), 2):
rods[int(rings[i+1])].add(rings[i])
for j in rods:
if len(j)>=3:
ans+=1
return ans

Submissions:

Reference:

https://leetcode.com/problems/rings-and-rods/discuss/1624322/Python3-array-of-sets

--

--