본문 바로가기
백준 - 파이썬/단계별 - 6 (심화1)

*[백준/파이썬] 1157번 단어 공부

by miiinn 2025. 5. 9.

 

리스트를 다루는 방법을 익히기 좋은 문제이다.

 

# 단어 공부

# lower()-소문자로 저장 / upper()-대문자로 저장
words = list(input().upper())

# 중복 제거한 리스트
unique_words = list(set(words))

cnt_list =[]

# unique_words에 있는 알파벳 하나씩 원래 문장(words)에서 개수 세기
for i in unique_words:
    cnt = words.count(i)
    cnt_list.append(cnt) # 각 알파벳 개수 append

if cnt_list.count(max(cnt_list)) > 1:
    print('?')
else:
    max_index = cnt_list.index(max(cnt_list))
    print(unique_words[max_index])

 

[핵심 코드]

words = list(input().upper())

-> upper함수

 

unique_words = list(set(words))

-> set함수로 중복을 제거한 후 다시 list로 형변환

 

max_index = cnt_list.index(max(cnt_list))

-> cnt_list에서 최댓값의 index를 가져오는 법