본문 바로가기

Coding Test/백준

[백준/자바] 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 br = new BufferedReader(new InputStreamReader(System.in));
        String word = br.readLine().toUpperCase();

        int[] count = new int[26];
        char[] arr = word.toCharArray();
        for(char c : arr){
            int index = c - 65;
            count[index]++;
        }

        int bigger = 0;
        for(int i : count){
            if(i > bigger) bigger = i;
        }

        StringBuilder sb = new StringBuilder();
        int num = 0;
        for(int i=0; i<26; i++){
            if(count[i] == bigger) {
                char c = (char) (i + 65);
                sb.append(c);
                num++;
            }
        }

        if(num == 1) System.out.println(sb);
        else System.out.println("?");

        br.close();
    }
}

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String word = br.readLine().toUpperCase();

        int[] count = new int[26];
        char[] arr = word.toCharArray();
        for(char c : arr){
            int index = c - 65;
            count[index]++;
        }

        int bigger = 0;
        char c = '?';
        for(int i=0; i<26; i++){
            if(bigger < count[i]) {
                bigger = count[i];
                c = (char) (i + 65);
            } else if(count[i] == bigger) c = '?';
        }

        System.out.println(c);

        br.close();
    }
}

'Coding Test > 백준' 카테고리의 다른 글

[백준/자바] 9012 - 괄호  (0) 2023.05.11
[백준/자바] 2738 - 행렬 덧셈  (0) 2023.05.11
[백준/자바] 2444 - 별 찍기 - 7  (0) 2023.05.11
[백준/자바] 10828 - 스택  (0) 2023.05.11
[백준/자바] 2751 - 수 정렬하기 2  (0) 2023.05.09