Today I learned
2022/01/19
회고록
항해 99, 알고리즘 1주차
교재 : 파이썬 알고리즘 인터뷰
10장 해시테이블
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/
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))
해시 테이블 훈련