[Leetcode] 771. Jewels and Stones
2021. 1. 19. 16:46ㆍProblem Solving/LeetCode
Leetcode 771. Jewels and Stones
(3) Jewels and Stones - LeetCode
Solution 1
리스트와 파이썬의 if x in y
문법을 이용한 풀이.
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
count = 0
J = ' '.join(jewels).split()
for s in stones:
if s in J:
count += 1
return count
Solution 2
collections
모듈의 Counter
이용한 풀이
import collections
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
count = 0
freq = collections.Counter(stones)
for J in jewels:
count += freq[J]
return count
- collections.Counter 이용한 빈도수 세기 - 문자열에도 적용 된다.
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Leetcode 46. Permutations (0) | 2021.01.29 |
---|---|
[Leetcode] 347. Top K Frequent Elements (0) | 2021.01.26 |
[Leetcode] 706. Design HashMap (0) | 2021.01.19 |
[Leetcode] 641. Design Circular Deque (0) | 2021.01.19 |
[Leetcode] 622. Design Circular Queue (0) | 2021.01.18 |