[BOJ] 1157. 단어공부 - Array

제출일 : 2019-10-08

문제 풀이 시간 : 5M

난이도 : ☆

Problem

link : https://www.acmicpc.net/problem/17472

Input

첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.

Output

첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.

Example

input

Mississipi

output

?

Solution & Inpression

알파벳은 26개

Code

언어 : JAVA

메모리 : 32,220 kb

실행시간 : 332 ms

import java.util.Arrays;
import java.util.Scanner;

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

        int[] cnt = new int[26];

        for (int i = 0; i < arr.length; i++) {
            cnt[arr[i]-'A']++;
        }
        int max = -1;
        int maxIdx = -1;
        for (int i = 0; i < cnt.length; i++) {
            if(max<cnt[i]) {
                max = cnt[i];
                maxIdx = i;
            }
        }

        for (int i = 0; i < cnt.length; i++) {
            if(maxIdx!=i) {
                if(max==cnt[i]) {
                    System.out.println("?");
                    return;
                }            
            }
        }

        System.out.println((char)('A'+maxIdx));
    }
}