771. jewels and stones

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”.

Example 1: Input: jewels = “aA”, stones = “aAAbbbb” Output: 3

  • code
class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
        return sum(i in jewels for i in stones)

  • code
class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
        count_stone = collections.Counter(stones)
        return sum(count_stone[i] for i in jewels)

  • code
class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
        return len(list(filter(lambda x: x in jewels, stones)))