본문 바로가기

Algorithm/Baekjoon

[백준_JAVA] 1157 단어 공부

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next().toUpperCase();

        int[] count = new int[26];
        int max = 0;
        char answer = '?';
        int num;
        
        for (int i = 0; i < str.length(); i++) {
            num = str.charAt(i) - 'A';
            count[num]++;
        }

        for (int i = 0; i < count.length; i++) {
            if(max < count[i]){
                max = count[i];
                answer = (char)(i+'A');
            } else if (max == count[i]){
                answer = '?';
            }
        }
        System.out.println(answer);
        sc.close();
    }
}

toUpperCase 메서드를 이용하여 모든 알파벳을 대문자로 취급했다.