Meta Data#
Difficulty: easy First Attempt: 2022-09-06
- Submissions: Accepted → Runtime Error ×2 → Accepted
Approach#
Turn jewels into a set and check each stone in O(1) (Hash Table). Case matters.
My Attempts#
The two REs used the old parameter names J and S (NameError); renaming to jewels and stones fixed it.
Solution#
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
jewel_set = set(jewels)
return sum(s in jewel_set for s in stones)
Complexity#
Time O(n + m), space O(m).
Mistakes to Avoid#
- Uppercase and lowercase are different stones.
Interview Explanation#
I put jewels in a set and count the stones that appear in it, in O(n + m).
