[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

Interesting in any computer science.

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

Recommended from Medium

6 Traits To Show To Ace That Behavioral Interview

Male celebrating.

What Can Be Done About Null? — Cantina

CSS Dynamic Columns

What is Git? Why or How to Use?

A guide to safely and efficiently test code that uses repositories

A hammer with nails

Building high performance, scalable web applications

Why Tutorials Won’t Make You a Professional Developer

Home work station

Let’s Learn Open-GL

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.

More from Medium

[LeetCode]#2068. Check Whether Two Strings are Almost Equivalent

BFS — Breadth First Search PT.2

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Regular Expression Matching…

4-in-1 Priority First Search in Python: BFS, DFS, Prim’s, and Dijkstra’s algorithms