LeetCode 2103: Rings and Rods (Bitmask Per Rod + Color Presence Count)
LeetCode 2103BitmaskSimulationToday we solve LeetCode 2103 - Rings and Rods.
Source: https://leetcode.com/problems/rings-and-rods/
English
Problem Summary
We are given an even-length string rings. Every two characters describe one ring: color (R/G/B) and rod index (0..9). Return how many rods finally contain all three colors.
Key Insight
Each rod has only three color states, so a 3-bit mask is perfect:
R = 1, G = 2, B = 4. If a rod has all colors, its mask becomes 1|2|4 = 7.
Algorithm
1) Create an array state[10], initialized to 0.
2) Scan rings by step 2: read color and rod.
3) OR the rod mask with this color bit.
4) Count rods with mask exactly 7.
Complexity Analysis
Time: O(n), where n is rings.length.
Space: O(1) (fixed size 10 rods).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int countPoints(String rings) {
int[] state = new int[10];
for (int i = 0; i < rings.length(); i += 2) {
char c = rings.charAt(i);
int rod = rings.charAt(i + 1) - '0';
int bit = (c == 'R') ? 1 : (c == 'G' ? 2 : 4);
state[rod] |= bit;
}
int ans = 0;
for (int mask : state) {
if (mask == 7) ans++;
}
return ans;
}
}func countPoints(rings string) int {
state := make([]int, 10)
for i := 0; i < len(rings); i += 2 {
color := rings[i]
rod := int(rings[i+1] - '0')
bit := 0
if color == 'R' {
bit = 1
} else if color == 'G' {
bit = 2
} else {
bit = 4
}
state[rod] |= bit
}
ans := 0
for _, mask := range state {
if mask == 7 {
ans++
}
}
return ans
}class Solution {
public:
int countPoints(string rings) {
int state[10] = {0};
for (int i = 0; i < (int)rings.size(); i += 2) {
char c = rings[i];
int rod = rings[i + 1] - '0';
int bit = (c == 'R') ? 1 : (c == 'G' ? 2 : 4);
state[rod] |= bit;
}
int ans = 0;
for (int i = 0; i < 10; ++i) {
if (state[i] == 7) ++ans;
}
return ans;
}
};class Solution:
def countPoints(self, rings: str) -> int:
state = [0] * 10
for i in range(0, len(rings), 2):
c = rings[i]
rod = ord(rings[i + 1]) - ord('0')
bit = 1 if c == 'R' else (2 if c == 'G' else 4)
state[rod] |= bit
return sum(1 for mask in state if mask == 7)var countPoints = function(rings) {
const state = new Array(10).fill(0);
for (let i = 0; i < rings.length; i += 2) {
const c = rings[i];
const rod = rings.charCodeAt(i + 1) - 48;
const bit = c === 'R' ? 1 : (c === 'G' ? 2 : 4);
state[rod] |= bit;
}
let ans = 0;
for (const mask of state) {
if (mask === 7) ans++;
}
return ans;
};中文
题目概述
给定一个长度为偶数的字符串 rings,每两个字符表示一个圆环:颜色(R/G/B)和柱子编号(0..9)。返回最终同时拥有三种颜色圆环的柱子数量。
核心思路
每个柱子只关心三种颜色是否出现过,用 3 位 bitmask 最合适:
R = 1,G = 2,B = 4。当柱子同时拥有三色时,mask 就是 7。
算法步骤
1)准备长度为 10 的数组 state,初始全 0。
2)每次读取两个字符:颜色 + 柱子编号。
3)把对应颜色 bit OR 到该柱子的状态里。
4)最后统计 mask 等于 7 的柱子个数。
复杂度分析
时间复杂度:O(n),n 为字符串长度。
空间复杂度:O(1)(固定 10 根柱子)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int countPoints(String rings) {
int[] state = new int[10];
for (int i = 0; i < rings.length(); i += 2) {
char c = rings.charAt(i);
int rod = rings.charAt(i + 1) - '0';
int bit = (c == 'R') ? 1 : (c == 'G' ? 2 : 4);
state[rod] |= bit;
}
int ans = 0;
for (int mask : state) {
if (mask == 7) ans++;
}
return ans;
}
}func countPoints(rings string) int {
state := make([]int, 10)
for i := 0; i < len(rings); i += 2 {
color := rings[i]
rod := int(rings[i+1] - '0')
bit := 0
if color == 'R' {
bit = 1
} else if color == 'G' {
bit = 2
} else {
bit = 4
}
state[rod] |= bit
}
ans := 0
for _, mask := range state {
if mask == 7 {
ans++
}
}
return ans
}class Solution {
public:
int countPoints(string rings) {
int state[10] = {0};
for (int i = 0; i < (int)rings.size(); i += 2) {
char c = rings[i];
int rod = rings[i + 1] - '0';
int bit = (c == 'R') ? 1 : (c == 'G' ? 2 : 4);
state[rod] |= bit;
}
int ans = 0;
for (int i = 0; i < 10; ++i) {
if (state[i] == 7) ++ans;
}
return ans;
}
};class Solution:
def countPoints(self, rings: str) -> int:
state = [0] * 10
for i in range(0, len(rings), 2):
c = rings[i]
rod = ord(rings[i + 1]) - ord('0')
bit = 1 if c == 'R' else (2 if c == 'G' else 4)
state[rod] |= bit
return sum(1 for mask in state if mask == 7)var countPoints = function(rings) {
const state = new Array(10).fill(0);
for (let i = 0; i < rings.length; i += 2) {
const c = rings[i];
const rod = rings.charCodeAt(i + 1) - 48;
const bit = c === 'R' ? 1 : (c === 'G' ? 2 : 4);
state[rod] |= bit;
}
let ans = 0;
for (const mask of state) {
if (mask === 7) ans++;
}
return ans;
};
Comments