Algorithm/백준

[BAEKJOON] 10808번: 알파벳 개수

NegotiationMan 2023. 9. 4. 20:29

문제

알파벳 소문자로만 이루어진 단어 S 주어진다. 알파벳이 단어에 개가 포함되어 있는지 구하는 프로그램을 작성하시오.

 

코드

import sys

input = sys.stdin.readline

# 딕셔너리를 이용해서 알파벳 개수 세기
abc = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0,
       'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0, }
str_ = input().rstrip()
for i in str_:
    abc[i] += 1

print(*abc.values())

# 배열을 이용해서 알파벳 개수 세기
abc = [0] * 26
for s in sys.stdin.readline().rstrip():
	# abc[ord(s) - 97] += 1 도 가능
    abc[ord(s) - ord('a')] += 1

print(*abc)

 

나의 생각

딕셔너리로 알파벳을 다 적는거 보다 배열을 이용해서 더욱 더 쉽게 풀 수 있다.