File: rings-and-rods/solution.py

Date: 2026-06-06

Time: 18:56

Purpose

This file is a self-contained solution to LeetCode 2103: Rings and Rods. It owns both the algorithm implementation and its test suite in a single module — the standard pattern across this repository's ~500+ problem directories.

The problem: given a string encoding color-rod pairs (e.g., "B0R0G0"), count how many rods have all three colors (Red, Green, Blue) placed on them.

Key Components

Solution.countPoints(rings: str) -> int

The core algorithm. Takes a string where each pair of characters represents a color (R/G/B) followed by a rod number (0-9), and returns the count of rods that collected all three colors.

TestSolution

Eight test cases covering: the three LeetCode examples, full coverage (all 10 rods with all colors), single-rod edge cases, duplicate colors on one rod, two-color insufficiency, and multi-rod qualifying scenarios.

Patterns

Set accumulation with defaultdict(set) — Each rod maps to a set of colors. Using a set automatically deduplicates repeated colors on the same rod, which is exactly what the problem requires. The final check len(colors) == 3 works because there are exactly three possible colors.

Stride-2 iterationrange(0, len(rings), 2) walks the string in pairs. This avoids explicit parsing or regex — the input format guarantees alternating color-char/digit-char pairs, so fixed-stride indexing is the cleanest approach.

Counting via generator expressionsum(1 for ... if ...) is the idiomatic Python pattern for conditional counting without materializing an intermediate list.

Dependencies

Imports: collections.defaultdict (core data structure), unittest (test harness).

Imported by: The test_solution.py in this same directory, plus the "Imported By" list in the prompt is misleading — that list appears to be an artifact of the repository tooling referencing a shared test runner or import pattern across all problem directories, not actual imports of this specific solution.

Flow

1. Initialize rods as a defaultdict(set) — keys are rod identifiers (single-char strings '0''9'), values are sets of color characters.

2. Walk rings two characters at a time: extract the color at position i and the rod at position i+1.

3. Add each color to its rod's set (duplicates are absorbed by the set).

4. Count and return the number of rods whose color set has exactly 3 elements.

For input "B0R0G0R9": after the loop, rods = {'0': {'B', 'R', 'G'}, '9': {'R'}}. Rod '0' has 3 colors → counts. Rod '9' has 1 → doesn't. Returns 1.

Invariants

Error Handling

None. The solution trusts the problem's input constraints. An odd-length string would silently produce a wrong answer (the last character would be treated as a color with no rod). An empty string returns 0 correctly since rods stays empty.

Topics to Explore

Beliefs