[LeetCode]#2103. Rings and Rods

  • 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').
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.
  1. Use set array
  2. Search each location rod its R, G, B rings as below.
  3. Return each set length =>3.
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

--

--

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
Fatboy Slim

Fatboy Slim

Interesting in any computer science.