329. Jewels and Stones

Easy
Membership Counting
Hashing

Problem

You are given strings jewels and stones. Every character in stones represents a stone you have, while every character in jewels represents a type of stone considered a jewel. Letters are case-sensitive. Return how many stones are jewels.

Examples

Example 1

Input: "aA", "aAAbbbb"
Output: 3

Example 2

Input: "z", "ZZ"
Output: 0

Example 3

Input: "abc", "aabbcc"
Output: 6
Constraints

1 <= jewels.length, stones.length <= 50

jewels and stones consist of English letters.

All characters in jewels are unique.

Hints

?? Store all jewel characters in a set.

?? Scan stones and count membership hits.

Expected Complexity
Time: O(jewels.length + stones.length)
Space: O(number of jewel types)
Follow-up

Can you solve it using a fixed lookup table instead of a hash set?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"aA", "aAAbbbb"
Expected
3