문제) J는 보석이며, S는 갖고 있는 돌이다. S에는 보석이 몇개나 있을까? 대소문자는 구분한다.
https://leetcode.com/problems/jewels-and-stones/
풀이) collections.Counter을 이용하면 쉽게 풀린다.
import collections
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
freqs = collections.Counter(stones)
return sum(list(freqs[j] for j in jewels))