https://www.acmicpc.net/problem/1157
나는 일일이 하나씩 대,소문자를 비교해가면서 하나씩 체크를 했는데, 이것보다 훨씬 더 간편한 방법이 있었다. 먼저 모두 대문자로 만들고, 각 알파벳을 숫자로 매칭시켜서 int array에 담은 뒤 비교하면 훨씬 간단하게 체크를 할 수 있었다.
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String word = scanner.next().toUpperCase(); //출력 형태가 대문자이므로 모두 대문자로 변환해서 처리해도 무방
int cnt[] = new int[26]; //각 알파벳의 개수를 저장할 int형 배열 총 26개를 생성(A to Z)
int max=0;
char answer ='?';
for(int i=0 ; i<word.length() ; i++) {
cnt[word.charAt(i)-65]++;
if(cnt[word.charAt(i)-65] > max) { //각 배열에 개수를 하나씩 셀 때마다 max와 비교 및 갱신
max = cnt[word.charAt(i)-65];
answer = word.charAt(i);
}
else if(cnt[word.charAt(i)-65] == max)
answer = '?';
}
System.out.print(answer);
scanner.close();
}
}