771. Jewels and Stones
Easy
Problem:
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3Solution:
Hash Table
def numJewelsInStones(self, jewels: str, stones: str) -> int:
freqs = {}
count = 0
# Calculate the frequency of S
for char in stones:
if char not in freqs:
freqs[char] = 1
else:
freqs[char] += 1
# Calculate the frequency of J
for char in jewels:
if char in freqs:
count += freqs[char]
return countDefaultdict
Counter
Pythonic way
Last updated