Skip to content

2103. Rings and Rods 👍

  • Time: $O(n)$
  • Space: $O(10) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int countPoints(string rings) {
    vector<int> colors(10);

    for (int i = 0; i < rings.length(); i += 2) {
      const int c = rings[i];
      const int color = c == 'R' ? 1 : c == 'G' ? 2 : 4;
      colors[rings[i + 1] - '0'] |= color;
    }

    return ranges::count(colors, 7);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int countPoints(String rings) {
    int[] colors = new int[10];

    for (int i = 0; i < rings.length(); i += 2) {
      final char c = rings.charAt(i);
      final int color = c == 'R' ? 1 : c == 'G' ? 2 : 4;
      colors[rings.charAt(i + 1) - '0'] |= color;
    }

    return (int) Arrays.stream(colors).filter(c -> c == 7).count();
  }
}
1
2
3
4
5
6
7
8
9
class Solution:
  def countPoints(self, rings: str) -> int:
    colors = [0] * 10

    for c, num in zip(rings[::2], rings[1::2]):
      color = 1 if c == 'R' else 2 if c == 'G' else 4
      colors[int(num)] |= color

    return sum(color == 7 for color in colors)