코딩테스트/백준 (python)

백준 / 1157번 / 단어 공부 / python 파이썬

seulll 2024. 5. 29. 20:09

문제 : https://www.acmicpc.net/problem/1157

 

 

코드 

import sys
from collections import Counter

word = sys.stdin.readline().strip()
word = word.upper()
count = Counter(word)

m = [k for k,v in count.items() if max(count.values()) == v]

if len(m) == 1:
    print("".join(m))
else:
    print('?')

Counter를 사용해 딕셔너리로 푼 코드이다.

 

다른 풀이

word = input().upper()
word_list = list(set(word))

cnt = []
for i in word_list:
  count = word.count
  cnt.append(count(i))

if cnt.count(max(cnt)) > 1:
  print("?")

else:
  print(word_list[(cnt.index(max(cnt)))])

알파벳이 사용된 횟수를 리스트에 저장하는 방법으로도 풀 수 있다.