[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

BULK INSERT — dynamic SQL

Creating Fillable PDF Form Fields in Java Application

Why I want to become a software engineer and why I want to attend Holberton School

Make your own calculator in HTML, CSS, JAVASCRIPT

What Does String Pool Mean in Java?

Self-Development Software: What Do Bloggers Have to Show You?

GNU/Linux Distribution Test: Archcraft

Using SOPS and git hooks to share secrets — Part 2

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 Patterns Adventure 17 — Binary Search

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

Introduction to LinkedList

[LeetCode] 461. Hamming Distance