💡 Algorithm/백준

[JAVA ] 백준 #1157- 단어 공부

현주먹 2020. 8. 20. 16:38

https://www.acmicpc.net/problem/1157

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] asc = new int[26];

        String str = sc.nextLine().toUpperCase();

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            asc[ch - 'A']++;
        }

        int maxIndex = 0;
        int max = 0;
        char result ='?';

        for (int i = 0; i < asc.length; i++) {  //최대값 찾기
            if (asc[i] > max) {
                max = asc[i];
                maxIndex = i;                   //최대인덱스값
                result= (char) (maxIndex+'A'); //or (char)(i+'A);
            }else if(max==asc[i])
                result = '?';
        }
        System.out.println(result);
    }
}