PS/BOJ
[백준] 단어 공부
RUBY_루비
2024. 2. 14. 23:00
출처:: https://www.acmicpc.net/problem/1157
분류::문자열
1. 문제 이해 및 해결과정
2. 풀이방법
1) 아스키코드 이용, chr()
alpha = [ 0 for i in range(26)]
a = input()
for i in a :
alpha[ord(i.lower()) - 97] +=1
max = -1
maxindex = -1
maxcnt= 0
for i in range(26):
if alpha[i] > max:
max = alpha[i]
maxindex = i
for i in range(26):
if alpha[i] == max:
maxcnt += 1
if maxcnt > 1 :
print("?")
else :
print(chr(maxindex+97).upper())
2) counter most_common 함수 이용
from collections import Counter
a = Counter(input().upper())
if(len(a)>1 and a.most_common()[0][1] == a.most_common()[1][1]):
print("?")
else:
print(a.most_common()[0][0])
3. 오답원인
4. 알게된 점
- chr() : 아스키 코드를 가지고 있는 값 리턴
-collections.Counter(배열).most_common(n) : 배열을 세면서 최빈값 n개를 튜플 형태로 반환한다. ex) (1,2),(4,5)