The title

Given the string J for the type of gem in the stone, and the string S for the stone you own. Each character in S represents a type of stone you own, and you want to know how many of the stones you own are gems.

The letters in J are not repeated, and all characters in J and S are letters. The letters are case sensitive, so “A” and “A” are different types of stone.

Example 1: Input J = "aA", S = "aAAbbbb" Output: 3 Example 2: Input J = "z", S = "ZZ" Output: 0Copy the code

Note:

S and J contain up to 50 letters. The characters in J are not duplicated.

Their thinking

class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
        from collections import Counter
        counter = Counter(stones)
        res = 0
        for i in jewels:
            res += counter.get(i, 0)
        # print(Counter(stones))
        return res


if __name__ == '__main__':
    J = "aA"
    S = "aAAbbbb"
    result = Solution().numJewelsInStones(J, S)
    print(result)
Copy the code