티스토리 뷰
문제

J는 보석이며, S는 갖고 있는 돌이다.
S에는 보석이 몇 개나 있을까?
₩대소문자는 구분₩한다.
leetcode 771 - Jewels and Stones
코드
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
freqs = {}
count = 0
# 돌(S)의 빈도 수 계산
for char in S:
# 처음이면 1 저장
if char not in freqs:
freqs[char] = 1
else:
# 이미 있으면 +1
freqs[char] += 1
# 보석(J)의 빈도 수 합산
for char in J:
if char in freqs:
count += freqs[char]
return count
조금 더 파이썬스러운 코드
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
# 파이썬 컴프리헨션
return sum(s in J for s in S)
'알고리즘' 카테고리의 다른 글
[algorithm] 백준 9461 - 파도반 수열 (파이썬) (0) | 2022.04.09 |
---|---|
[algorithm] 백준 11726 - 2xn 타일링 (파이썬) (0) | 2022.04.04 |
[algorithm] leetcode 641 - Design Circular Deque (파이썬) (0) | 2022.03.25 |
[algorithm] leetcode 20 - Valid Parentheses (파이썬) (0) | 2022.03.24 |
[algorithm] leetcode 232 - Implement Queue using Stacks (파이썬) (0) | 2022.03.23 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- leetcode
- 노마드코더
- Real MySQL
- 김영한
- 릿코드
- 백준
- 코테
- 파이썬
- 인프런
- kotlin
- 그리디
- spring boot
- Spring
- 문자열
- 스프링부트
- webflux
- Algorithm
- 리팩토링
- mysql 8.0
- MySQL
- 알고리즘
- 북클럽
- 노마드
- 구현
- 스프링
- 자료구조
- 정렬
- 스프링 부트
- 코틀린
- 데이터베이스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함