항해99 2주차 - 보석과 돌

Jang Seok Woo·2022년 1월 23일
0

알고리즘

목록 보기
21/74

Today I learned
2022/01/19

회고록


1/19

항해 99, 알고리즘 1주차

교재 : 파이썬 알고리즘 인터뷰

10장 해시테이블

1. 이론

https://velog.io/@jsw4215/%ED%95%AD%ED%95%B499-2%EC%A3%BC%EC%B0%A8-%EB%B3%B4%EC%84%9D%EA%B3%BC-%EB%8F%8C

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. 총평

해시 테이블 훈련

profile
https://github.com/jsw4215

0개의 댓글