본문 바로가기
Algorithm/Beakjoon

[Java] baekjoon 1157 : 단어 공부 / 문자열

by Amy97 2021. 7. 27.
728x90

문제 


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

 

1157번: 단어 공부

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

www.acmicpc.net

구현 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
 
        String word = sc.next().toUpperCase(); 
        int[] alphabet = new int[26]; 
 
        for (int i = 0; i < word.length(); i++) {
            alphabet[word.charAt(i) - 'A']++;
        }
 
        int max = 0;
        int idx = 0;
        for (int i = 0; i < alphabet.length; i++) {
            if (alphabet[i] > max) {
                max = alphabet[i];
                idx = i;
            }
        }
        char res = (char) (idx + 'A');
 
        for (int i = 0; i < alphabet.length; i++) {
            if (max == alphabet[i] && idx != i) {
                res = '?';
            }
        }
        System.out.println(res);
    }
}
cs

결과 


728x90

댓글