백준 baekjoon

[백준 10808] 알바벳개수

니블 2023. 4. 26. 18:01

04-21 / 04-26

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

 

10808번: 알파벳 개수

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

www.acmicpc.net

1차 코드 (답코드) 

count 를 이용

x = list(str(input()))
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in range(len(alphabet)):
    y = x.count(alphabet[i])
    print(y, end=' ')

2차 코드 (참고만하고 직접 품) 

[0] *26 알파벳을 먼저 0으로 초기화 하고 바꿔주는 방식을 한번 구현해보고 싶었다. 

chat gpt 에 물어본부분

i ) ord(a) 97-96 

ii) 리스트 요소들을 리스트말고 공백을 포함한 int형으로 출력하기 

print(' '.join(list(map(str,alpha))))

join함수 

리스트 요소들을 원하는 대로 출력 (구분자 넣어서) 

만약 아래 코드 처럼 한다면 

my_list = ['apple','banna','orange','grape']
result =''
for item in my_list: 
    result += item 
print(result)

출력은 applebannaorangegrape 

 join함수를 이용하면 

my_list = ['apple','banna','orange','grape']
result = ', '.join(my_list)  # ' '.join으로 하면 공백포함해서 출력됨 
print(result)

apple,banna,orange,grape 

 

s = list(input()) 
alpha = [0]*26 #알파벳 리스트를 0으로 초기화 하는 방법 ? 

for i in range(len(s)): 
    n=ord(s[i])-96
    alpha[n-1]+=1 

print(' '.join(list(map(str,alpha))))