BOJ/String

[BOJ] 1157번 단어 공부

J_Jayce 2019. 8. 22. 11:54

문제

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
34
35
36
37
38
39
import java.util.*;
public class Main {
    static Scanner scan = new Scanner(System.in);
    static String test;
    static int[] cnt = new int[26];
    static String find_most_freq() {
        int max = -1;
        int idx = -1;
        //카운팅
        for(int i=0;i<test.length();i++
            cnt[test.charAt(i)-'A']++;
        
        //빈도수 확인
        for(int i=0;i<26;i++) {
            if(cnt[i]>max) {
                max = cnt[i];
                idx = i;
            }
        }
        
        //다수 최빈수 확인
        for(int i=0;i<26;i++) {
            if(i!=idx && cnt[i]==cnt[idx])
                return "?";
        }
        
        return ((char)('A'+idx))+"";
    }
    static void solution() {
        test = scan.nextLine().toUpperCase();
        System.out.println(find_most_freq());
    }
    
    
    public static void main(String[] args) {
        solution();
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter