
문제 분석
- 하나의 단어에 각 알파벳이 몇개 포함되어 있는지 return
코드 설계
- 문자를 아스키 코드로 변환하고
- 딕셔너리
- a ~ z 크기의 모두 값이 0인 리스트 생성 (26개)
- 단어를 하나씩 돌면서 아스키코드로 변환하고 a = 0이도록 변환 (ord)
- a == 97 → char - 97
- 해당 리스트의 인덱스 값 +1
정답 코드
1차 - count( ) (노가다..)
import sys
input = sys.stdin.readline
s = input()
print(s.count('a'), s.count('b'), s.count('c'), s.count('d'), s.count('e')
, s.count('f'), s.count('g'), s.count('h'), s.count('i'), s.count('j')
, s.count('k'), s.count('l'), s.count('m'), s.count('n'), s.count('o')
, s.count('p'), s.count('q'), s.count('r'), s.count('s'), s.count('t')
, s.count('u'), s.count('v'), s.count('w'), s.count('x'), s.count('y')
, s.count('z'))
2차
import sys
input = sys.stdin.readline
string = input().strip()
arr = [0] * 26 # 0~25
for s in string :
arr[ord(s)-97] += 1
for a in arr :
print(a, end=' ')
만약 이때 input( ).strip( ) 을 안붙이면 입력 마지막에 \n 이 함께 입력되어서 out of index 에러가 난다.
- 파이썬 출력 마지막 설정: end=' '
'코딩 테스트 > 백준' 카테고리의 다른 글
[Python] 백준 3273 두 수의 합 (0) | 2024.12.23 |
---|---|
[Python] 백준 1475 방 번호 (0) | 2024.12.23 |
[Python] 백준 7576 토마토 (0) | 2024.12.23 |
[Python] 백준 1463 1로 만들기 (1) | 2024.12.22 |
[Python] 백준 25304 영수증 (0) | 2024.12.22 |