백준 - 1157번(단어 공부)

최지홍·2022년 2월 5일
0

백준

목록 보기
12/145

문제 출처: https://www.acmicpc.net/problem/1157


문제

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] counts = new int[26];
        int max = -1;
        for (char c : reader.readLine().toCharArray()) {
            int temp = c - 65;
            if (c >= 97) {
                temp -= 32;
            }
            counts[temp]++;
            max = Math.max(max, counts[temp]);
        }

        int count = 0;
        int result = -1;
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] == max) {
                count++;
                result = i;
            }
        }

        if (count != 1) {
            System.out.println("?");
        } else {
            System.out.println((char) (65 + result));
        }
        reader.close();
    }

}

  • 알파벳 길이 만큼(26개) 배열을 만들어 숫자를 카운트
profile
백엔드 개발자가 되자!

0개의 댓글