Jewels and Stones - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def numJewelsInStones(jewels: str, stones: str-> int:
    d = dict()
    ans = 0
    for i in jewels:
        d[i] = 1
    for i in stones:
        if i in d.keys():
            ans += 1
    return ans
 
from collections import defaultdict
def numJewelsInStones(jewels: str, stones: str-> int:
    d = defaultdict(int)
    ans = 0
    for i in stones:
        d[i] += 1
    for i in jewels:
        ans += d[i]
    return ans
 
 
jewels = "aA"
stones = "aAAbbbb"
print(numJewelsInStones(jewels, stones))
cs

+ Recent posts