[백준 1157번] 단어 공부 (python)

728x90
반응형

most_common은 넘겨받은 수만큼 가장 많이 등장한 값을 전해준다

배열로 변환해서 만약 길이가 1이거나 가장 많이 사용된 알파벳 개수가 여러개가 아니면 해당 값을 출력하고

 

가장 많이 사용된 알파벳이 여러개라면 (두 개 이상일 테니 처음 두 개의 값만 비교하면 된다) ?을 출력한다

 

defaultdict으로 해도 되겠지만 most_common을 사용할 수 있기에 Counter를 사용했다

 

import sys
from collections import Counter
input=sys.stdin.readline

s=input().strip().upper()

cnt=Counter(s)
# print(len(cnt))
# print(list(cnt.most_common(2))[0][1],list(cnt.most_common(2))[1][1])
if len(cnt)==1 or list(cnt.most_common(2))[0][1]!=list(cnt.most_common(2))[1][1]:
    print(list(cnt.most_common(2))[0][0])
else:
    print('?')

 

728x90
반응형
TAGS.

Comments