알고리즘/백준-파이썬
[백준] 10808번 (python 파이썬)
배불뚱이
2022. 4. 8. 23:12
https://www.acmicpc.net/problem/10808
10808번: 알파벳 개수
단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.
www.acmicpc.net
s = input()
d = {}
for c in s: # {키:알파벳 값:개수} 딕셔너리 정의
d[c] = d.setdefault(c, 0) + 1
for i in range(ord("a"),ord("z")): # a부터 z앞까지 개수를 출력
print(d.setdefault(chr(i), 0), end=" ")
else: # z개수를 출력
print(d.setdefault("z", 0)) # z만 따로 출력한 이유는 print() 마지막 공백 때문
또는 파이썬에서 지원해주는 collections 패키지의 Counter모듈을 사용해도 된다.
from collections import Counter
s = input()
counter = Counter(s)
for i in range(ord("a"),ord("z")):
print(counter.setdefault(chr(i), 0), end=" ")
else:
print(counter.setdefault("z", 0))