코딩테스트 풀이/JAVA
[백준 온라인 저지] 1157. 단어 공부
무지맘
2023. 3. 7. 16:17
- 입력: 첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
- 출력: 첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] input = br.readLine().toUpperCase().toCharArray();
int[] count = new int[26];
int max = 0, index = -1;
for(char c : input)
count[c-65]++;
for(int i=0 ; i<26 ; i++){
if(count[i]>max){
max = count[i]; index = i;
}
}
String answer = String.valueOf((char)(index+65));
for(int i=0 ; i<26 ; i++){
if(count[i]==max && index!=i){
answer = "?"; break;
}
}
System.out.println(answer);
}
}