항해99 2주차 - 보석과 돌
Today I learned
2022/01/19
회고록
1/19
항해 99, 알고리즘 1주차
교재 : 파이썬 알고리즘 인터뷰
10장 해시테이블
1. 이론
2. 문제
You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ"
Output: 0
Constraints:
1 <= jewels.length, stones.length <= 50
jewels and stones consist of only English letters.
All the characters of jewels are unique.
https://leetcode.com/problems/jewels-and-stones/
3. MySol
def solution(jewel, stone):
hash = {}
for s in stone:
if s not in hash:
hash[s] = 1
else:
hash[s] += 1
count=0
for j in jewel:
if j in hash:
count+=hash[j]
return count
if __name__ == '__main__':
J = 'z'
S = 'ZZ'
result = solution(J,S)
print('result : ' + str(result))
4. 배운 점
- 해시 테이블 이해 및 응용
5. 총평
해시 테이블 훈련
Author And Source
이 문제에 관하여(항해99 2주차 - 보석과 돌), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jsw4215/항해99-2주차-보석과-돌-0l1l1zxv저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)