카테고리 없음

가장 많은 글자

o_omi 2022. 6. 20. 01:50

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

import java.io.*;
import java.util.*;

public class Main {

    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {

        //a~z : 97~122
        HashMap<Integer,Integer> alphaMap = new HashMap<Integer,Integer>();

        // 97 : a, 98 : b ... 122 : z
        for(int i = 97; i<=122; i++){
            alphaMap.put(i,0);
        }

        StringBuilder testCase = new StringBuilder();

        String line = "";
        while ((line = br.readLine()) != null){
            //모든 공백 제거
            line = line.replace(" ","").trim();
            testCase.append(line);
        }

        for(int i = 0; i < testCase.length(); i++){
            int askii = testCase.charAt(i);
            int value = alphaMap.get(askii);
            alphaMap.put(askii, value+1);
        }

        int max = Collections.max(alphaMap.values());
        String alphabet = "";

        for (int i : alphaMap.keySet()) {
            if(alphaMap.get(i) == max){
                alphabet += (char)i;
            }
        }

        bw.write(alphabet);

        br.close();
        bw.close();
    }

}