117. Gem Counter
A miner keeps a list of precious gem types in the string jewels: each distinct letter stands for one type, and lower case and upper case letters are different types. The string stones describes everything found in a cart, one letter per stone.
Return how many of the stones in the cart are of a type listed in jewels. Each stone is counted once, even if it is found several times in the list. Use a set for the types so the answer takes linear time.
Example 1
- Input:
- jewels = "aB", stones = "aabbBBB"
- Output:
- 5
- Explanation:
The stones a, a, B, B and B are gems, so five stones count; the two b stones do not.
Example 2
- Input:
- jewels = "z", stones = "abc"
- Output:
- 0
- Explanation:
The cart has no z stone, so the answer is 0.
Constraints
1 ≤ jewels.length, stones.length ≤ 5 * 104
Both strings contain only English letters
All letters of jewels are distinct
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n + m)
- Space
- O(1)