문제요약
나의 코드 및 설명
- string = list(input().upper()) : 입력 받은 문자열을 대문자로 변환하여, 리스트에 저장한다.
- string_list : 입력 받은 문자열을 중복 없이 리스트에 저장한다.
- string에 저장된 문자열에 문자가 각각 몇 개씩 등장하는지 cnt 리스트에 담는다. (string의 인덱스와 cnt의 인덱스는 일치한다.)
- cnt에 저장된 cnt의 최댓값이 2개 이상인 경우 "?"를 출력한다.
string = list(input().upper())
string_list = list(set(list(map(str, str(string)))))
cnt = []
for i in range(len(string_list)):
cnt.append(string.count(string_list[i]))
if cnt.count(max(cnt)) >= 2:
print("?")
else:
print(string_list[cnt.index(max(cnt))])
다른 코드 및 설명
- 전반적인 풀이 과정은 비슷하나, 반복문에서 리스트의 원소들을 i로 받아 보다 간결하게 코드를 작성하였다.
word = input().upper()
word_list = list(set(word))
cnt = []
for i in word_list:
cnt.append(word.count(i))
if cnt.count(max(cnt)) >= 2:
print("?")
else:
print(word_list[cnt.index(max(cnt))])
피드백
내가 작성한 코드가 다른 코드보다 실행 시간이 오래걸렸는데, 아래와 같이 수정하니 줄어들었다.
차이는, 1. string을 굳이 리스트에 저장하지 않았다. 2.반복문에서 리스트의 원소들을 i로 받았다.
string = input().upper()
string_list = list(set(string))
cnt = []
for i in string_list:
cnt.append(string.count(i))
if cnt.count(max(cnt)) >= 2:
print("?")
else:
print(string_list[cnt.index(max(cnt))])
'Baekjoon > IM Level' 카테고리의 다른 글
[백준] 2739 구구단 (브론즈5) / .format , 재귀함수로 풀기 (0) | 2023.03.01 |
---|---|
[백준] 2577 숫자의 개수 (브론즈2) / 숫자 각 자릿수를 리스트에 저장 (0) | 2023.03.01 |
[백준] 2164 카드2 (실버4) / deque() (0) | 2023.03.01 |
[백준] 2851 슈퍼마리오 (브론즈1) (0) | 2023.02.24 |
[백준] 2810 컵홀더 (브론즈1) (0) | 2023.02.24 |