[BOJ]10808(O)

2024. 7. 28. 15:02Algorithm

1. problem : 

https://www.acmicpc.net/problem/10808

 

2. solution 1 : 

import string 
n = input()
alphabet = string.ascii_lowercase
alphabet_dic = {letter:0 for letter in (alphabet)}
for char in n:
     alphabet_dic[char] += 1 

print(" ".join(str(alphabet_dic[letter]) for letter in alphabet))

string을 import 해서 alphabet을 받는다. 그런 다음 딕셔너리를 만들고 , input의 각각의 알파벳들을 count 한 다음, value값을 출력한다. 

 

3. solution 2 :

from collections import Counter
import string

# 사용자 입력 받기
n = input()

# 입력 문자열의 각 문자의 개수 세기
counter = Counter(n)

# 알파벳 목록 생성
alphabet = string.ascii_lowercase

# 결과 출력
# 알파벳 순서대로 개수를 출력, 없는 알파벳은 0으로 출력
print(" ".join(str(counter[letter]) for letter in alphabet))

Counter를 이용한다. Counter는 특정 string을 넣었을 때, 자동으로  dictionary로 만들어준다. 매우 편함. 

그다음 출력은 똑같다. 

 

4.  solution 3 :

x = input() 
ans = [0] * 26
for s in x:
     index = ord(s) - 97
     ans[index] += 1 
print(" ".join(map(str,ans)))

ord(문자)를 이용하는 풀이다. alphabet이 다 붙어있기 때문에 , 효율적이다. ord("a")는 97이다. 그래서 -97을 빼서, 0으로 만든다.