본문 바로가기
코딩 테스트/백준

단어 공부

by ornni 2024. 11. 18.
728x90
반응형

첫번쨰 코드

 

일단 모든 결과는 대문자로 나와야 하므로 모두 대문자로 저장한다.

 

확실이 문자별로 개수를 셀 때에는 딕셔너리를 이용해서 저장하고 불러오는 것이 속도가 빠르다.

하여 모든 문자가 나오는 개수를 딕셔러니에 저장한다.

 

이후 최대로 나오는 값을 확인한 후

해당 알파벳의 개수가 1개인 경우 해당 알파벳을 불러오고

1개 이상인 경우 ?을 출력하는 형식으로 답안을 작성하였다.

 

word = input()

word = word.upper()
alpha_count = dict()
answer = []

for i in word:
    if i in alpha_count.keys():
        alpha_count[i] += 1
    else:
        alpha_count[i] = 1

max_count = max(alpha_count.values())

for i, j in alpha_count.items():
    if j == max_count:
        answer.append(i)

if len(answer) == 1:
    print(answer[0])
else:
    print('?')

 

통과!


링크

https://github.com/ornni/programmers/tree/main/%EB%B0%B1%EC%A4%80/Bronze/1157.%E2%80%85%EB%8B%A8%EC%96%B4%E2%80%85%EA%B3%B5%EB%B6%80

 

programmers/백준/Bronze/1157. 단어 공부 at main · ornni/programmers

repository for recording Programmers Algorithm problem solving - ornni/programmers

github.com

 

반응형

'코딩 테스트 > 백준' 카테고리의 다른 글

벌집  (0) 2024.11.29
ZOAC 4  (0) 2024.11.15
삼각형과 세 변  (0) 2024.11.11
호텔 방 번호  (1) 2024.09.23
사과 담기 게임  (0) 2024.09.20