Problem statement

https://leetcode.com/problems/jewels-and-stones/

Solution

Just check for each element in S that it is in J.

Complexity

Time complexity is O(m + n), where m is length of S and n is length of J. Space complexity is O(n).

Code

class Solution:
    def numJewelsInStones(self, J, S):
        set_J = set(J)
        return sum(s in set_J for s in S)