Jewels and Stones (LeetCode 771): Solution and Explanation

Easy
Hash Table
String

Each character of jewels is a stone type that counts as a jewel. Count how many characters of stones are one of those types, treating uppercase and lowercase as different.

Intuition

For every stone the only question is whether its type appears in jewels. Scanning the whole jewels string for each stone works, but it repeats the same search over and over.

Put the jewel types into a hash set once. After that, asking whether a stone is a jewel takes constant time, so one pass over the stones gives the count.

Case matters: a and A are different characters, so do not lowercase anything. A stone Z is not a jewel when only z is.

Approach: Hash set of jewel types

  1. Build jewelSet from the characters of jewels.
  2. Set count = 0.
  3. For each character s in stones, add 1 to count if s is in jewelSet.
  4. Return count.

Step-by-step example

jewels = "aA", stones = "aAAbbbb"
  1. The jewel set is {a, A} and count starts at 0.
  2. Stone a is in the set, so count = 1.
  3. Stone A is in the set, so count = 2.
  4. The next A is also in the set, so count = 3.
  5. The four b stones are not in the set, so count stays 3 and the function returns 3.

Code: Python, Java, C++

1class Solution:
2    def numJewelsInStones(self, jewels: str, stones: str) -> int:
3        jewel_set = set(jewels)
4        count = 0
5        for s in stones:
6            if s in jewel_set:
7                count += 1
8        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(|J| + |S|)

Building the set touches each jewel once and the scan touches each stone once, with O(1) set lookups.

Space

O(|J|)

The set holds at most one entry per jewel type, which is at most 52 letters.

Other approaches

Nested loops (time O(|J| * |S|), space O(1))

For every stone, scan the jewels string for a match. Simple and fine for the small limits here, but quadratic in general.

Fixed boolean table (time O(|J| + |S|), space O(1))

Use a 128-entry boolean array indexed by character code instead of a hash set. Same idea, slightly faster in practice because there is no hashing.

Common mistakes

  • Lowercasing the input, which turns a non-jewel like Z into a jewel when z is one.
  • Using a 26-slot array indexed by c - 'a', which breaks on uppercase letters.
  • Building the set from stones and counting jewels, which counts each type once instead of counting every stone.

Frequently asked questions

Why use a set when jewels has at most 50 characters?

With these limits nested loops also pass. The set shows the general pattern: turn the lookup side into a hash structure so each query costs O(1).

Are uppercase and lowercase letters the same jewel?

No. The problem is case sensitive, so a and A are different stone types.

Can jewels contain duplicates?

No, all characters of jewels are unique. Even if they were not, a set would ignore the repeats.

What is the time complexity of Jewels and Stones?

O(|J| + |S|) time and O(|J|) extra space with a hash set of jewel types.