File: jewels-and-stones/solution.py

Date: 2026-06-06

Time: 17:09

jewels-and-stones/solution.py

Purpose

This file solves LeetCode 771 — Jewels and Stones. It owns the single responsibility of counting how many characters in stones appear in jewels. Within the project, it follows the standard pattern: each problem gets a directory with solution.py, test_solution.py, plan.md, and review.md.

Key Components

numjewelsin_stones(jewels: str, stones: str) -> int — The sole public function. Contract:

Patterns

Set-based membership testing. The function converts jewels to a set on line 13, then uses a generator expression with sum() to count matches. This is the canonical Python idiom for "count elements of A that appear in B" — it trades O(J) space for O(1) per-lookup instead of O(J) per-lookup with a raw string in check.

Generator over list comprehension. sum(s in jewel_set for s in stones) uses a generator expression (no intermediate list), which is memory-efficient — it yields one boolean at a time. sum exploits the fact that True == 1 and False == 0 in Python's numeric tower.

Dependencies

Imports: None. Pure standard Python — no library dependencies.

Imported by: Extensively. The "Imported By" list shows ~400+ test files across the repo reference this module. That's almost certainly a tooling artifact — the test harness likely imports all solution modules dynamically or through a shared test runner (runtests.py), not because those other tests actually call numjewelsinstones.

Flow

1. Build jewel_set from jewels — O(J) time and space where J = len(jewels).

2. Iterate over every character in stones, testing membership in jewel_set — O(S) time where S = len(stones), O(1) per lookup (amortized).

3. sum() accumulates the boolean results and returns the total count.

Total complexity: O(J + S) time, O(J) space.

Invariants

Error Handling

None. Empty strings are handled naturally: an empty jewels produces an empty set (every membership test is False, sum is 0); an empty stones produces an empty generator (sum is 0). No exceptions are raised or caught.

Topics to Explore

Beliefs