#2103

Rings and Rods

newbie · 155 · lc easy +14 · verified · 81.5% accepted · 1,028 likes · top 93%

Description

There are n rings, and each ring is colored red, green, or blue. These rings are distributed among ten rods numbered 0 to 9.

You are given a string rings of length 2n encoding the ring placements. Every consecutive pair of characters describes one ring: the first character is the color ('R', 'G', or 'B') and the second is the rod number ('0' to '9').

For instance, "R3G2B1" means three rings: a red ring on rod 3, a green ring on rod 2, and a blue ring on rod 1.

Return the count of rods that hold rings of all three colors.

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.

Example 2:

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

Example 3:

Input: rings = "G4"
Output: 0
Explanation:
Only one ring is given. Thus, no rods have all three colors.

Code

1
2
3