Jewels and Stones - Problem

You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

Letters are case sensitive, so "a" is considered a different type of stone from "A".

Input & Output

Example 1 — Basic Case
$ Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
💡 Note: The stones 'a', 'A', and 'A' match jewel types in "aA". The letter 'b' is not a jewel type.
Example 2 — Case Sensitivity
$ Input: jewels = "z", stones = "ZZ"
Output: 0
💡 Note: Since letters are case sensitive, lowercase 'z' is different from uppercase 'Z'. No matches found.
Example 3 — All Match
$ Input: jewels = "abc", stones = "aabbcc"
Output: 6
💡 Note: All stones match jewel types: 'a'(2), 'b'(2), 'c'(2) = 6 total jewels.

Constraints

  • 1 ≤ jewels.length, stones.length ≤ 50
  • jewels and stones consist of only English letters
  • All the characters of jewels are unique

Visualization

Tap to expand
Jewels and Stones: Count Matching Stone TypesJewel Types"aA"Your Stones"aAAbbbb"aJewel ✓AJewel ✓AJewel ✓bNot jewelbbbCount stones that match jewel types (case sensitive)Result: 3 jewels found
Understanding the Visualization
1
Input
Jewels: "aA", Stones: "aAAbbbb"
2
Process
Check each stone against jewel types
3
Output
Count of matching stones: 3
Key Takeaway
🎯 Key Insight: Use a hash set to store jewel types for O(1) lookup time per stone
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
95.0K Views
High Frequency
~8 min Avg. Time
4.3K 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