자바/백준

[백준 1157] 단어 공부

슈슈버거 2022. 11. 3. 16:11

문제

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

 

1157번: 단어 공부

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

www.acmicpc.net


내가 작성한 코드

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class qor1157 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String input = br.readLine().toUpperCase();
        ArrayList<Character> alpha = new ArrayList<>();

        for(int i = 0; i<input.length(); i++){
            alpha.add(input.charAt(i));
        }

        alpha.sort(null);
        String result = "";

        for(int i = 0; i< alpha.size(); i++){
            if(!result.contains(String.valueOf(alpha.get(i)))){
                result += alpha.get(i);
            }
        }

        int[] num = new int[result.length()];
        int j = 0;

        for(int i = 0; i< alpha.size()-1; i++){
            if(alpha.get(i).equals(alpha.get(i+1))){
                num[j]++;
            }
            else{
                j++;
            }
        }

        int max = 0;
        for(int i = 0; i<num.length; i++){
            if(num[i]>max){
                max = num[i];
            }
        }

        String word = "";
        for(int i = 0; i<num.length; i++){
            if(num[i]==max){
                word += result.charAt(i);
            }
        }

        if(word.length()!=1){
            bw.write("?");
        }
        else{
            bw.write(word);
        }

        bw.close();
    }
}