[LeetCode]#2103. Rings and Rods
Environment: Python 3.8
Key technique: set, if, for
n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from
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
ithpair denotes the
ithring's color (
- The second character of the
ithpair denotes the rod that the
ithring is placed on (
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.
Input: rings = "B0B6G0R6R0R6G9"
- 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.
- Use set array
- Search each location rod its R, G, B rings as below.
- Return each set length =>3.
def countPoints(self, rings: str) -> int:
rods = [set() for _ in range(10)]
for i in range(0, len(rings), 2):
for j in rods: