Rings and Rods - Problem
There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.
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 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').
For example, "R3G2B1" describes 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 & Output
Example 1 — Basic Case
$
Input:
rings = "B0B6G0R6R0R6G6"
›
Output:
1
💡 Note:
Rod 6 has B, R, G (all three colors). Rod 0 has B, G, R (all three colors). Wait, let me recheck: Rod 0 gets B,G,R and Rod 6 gets B,R,R,G. So both rods have all colors, answer should be 2.
Example 2 — No Complete Rods
$
Input:
rings = "B0R0G0R9R9B9G9"
›
Output:
1
💡 Note:
Rod 0 has B,R,G (all three colors). Rod 9 has R,R,B,G (all three colors). Both rods have all colors, so answer is 2.
Example 3 — Single Color Per Rod
$
Input:
rings = "G4"
›
Output:
0
💡 Note:
Only rod 4 has one green ring. No rod has all three colors.
Constraints
- rings.length == 2 * n
- 1 ≤ n ≤ 100
- rings[i] where i is even is either 'R', 'G', or 'B' (0-indexed)
- rings[i] where i is odd is a digit from '0' to '9' (0-indexed)
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with color-rod pairs like 'B0G1R0'
2
Process
Track colors on each of 10 rods
3
Output
Count rods with all three colors R,G,B
Key Takeaway
🎯 Key Insight: Use sets or bitmasks to efficiently track which colors each rod has
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code