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 |
'알고리즘 문제풀이 with 파이썬 > LeetCode' 카테고리의 다른 글
[해시] Top K Frequent Elements (0) | 2021.09.18 |
---|---|
[해시] Longest Substring Without Repeating Characters (0) | 2021.09.18 |
[스택] Daily Temperatures (0) | 2021.09.14 |
[스택] Remove Duplicate Letters (0) | 2021.09.13 |
[스택] Valid Parentheses (0) | 2021.09.12 |