Jewels and Stones - Problem

Imagine you're a treasure hunter who has collected a bag of various stones during your adventure. You also have a jeweler's guide that tells you which types of stones are actually precious jewels!

Given two strings:

  • jewels - represents the types of stones that are considered jewels (each character is a jewel type)
  • stones - represents all the stones you have collected (each character is a stone you own)

Your task is to count how many of your stones are actually jewels. Note that the problem is case-sensitive, meaning 'a' and 'A' are considered different types of stones.

Goal: Return the total count of stones that are also jewels.

Input & Output

example_1.py โ€” Basic Case
$ Input: jewels = "aA", stones = "aAAbbbb"
โ€บ Output: 3
๐Ÿ’ก Note: The stones 'a', 'A', and 'A' are jewels (case-sensitive). The stones 'b' are not jewels.
example_2.py โ€” No Matches
$ Input: jewels = "z", stones = "ZZ"
โ€บ Output: 0
๐Ÿ’ก Note: Since the problem is case-sensitive, 'z' and 'Z' are different. No stones match the jewels.
example_3.py โ€” All Matches
$ Input: jewels = "abc", stones = "aabbcc"
โ€บ Output: 6
๐Ÿ’ก Note: All stones are jewels: 2 'a's, 2 'b's, and 2 'c's = 6 total jewels.

Constraints

  • 1 โ‰ค jewels.length, stones.length โ‰ค 50
  • jewels and stones consist of only English letters
  • jewels has no duplicate characters
  • Both strings are case-sensitive

Visualization

Tap to expand
Jewels and Stones: Efficiency ComparisonโŒ Inefficient: Brute ForceaaAbcEach stone checks ALL jewel typesTime: O(n ร— m) = Many comparisonsLike asking jeweler about each stone individuallyโœ… Efficient: Hash SetQuickReference{a,A,b,c}aEach stone: ONE instant lookupTime: O(n + m) = Linear timeLike memorizing guide, then rapid checkingReal Example: jewels="aA", stones="aAAbbbb"Brute Force ProcessStone 'a': check 'a'โœ“, 'A'โœ—Stone 'A': check 'a'โœ—, 'A'โœ“Stone 'A': check 'a'โœ—, 'A'โœ“Stone 'b': check 'a'โœ—, 'A'โœ—...continue for all 7 stonesTotal: 14 operationsHash Set Process1. Build set: {a, A} (2 ops)2. Check 'a' in set: โœ“ (1 op)3. Check 'A' in set: โœ“ (1 op)4. Check 'b' in set: โœ— (1 op)...continue for all 7 stonesTotal: 9 operationsResult: 3 jewels found โ€ข Hash Set is 36% faster!
Understanding the Visualization
1
Prepare Reference
Create a quick-lookup reference (hash set) of all jewel types
2
Process Collection
Go through each stone in your collection once
3
Quick Check
For each stone, instantly check if it's a jewel using the reference
4
Count Results
Tally up all the jewels found in your stone collection
Key Takeaway
๐ŸŽฏ Key Insight: Converting the jewels into a hash set enables O(1) lookup time instead of O(m) linear search, reducing overall complexity from O(nร—m) to O(n+m)
Asked in
Google 45 Amazon 38 Microsoft 25 Meta 20
68.3K Views
High Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen