공부 흔적남기기

백준 1157 단어공부 자바 본문

코테/백준

백준 1157 단어공부 자바

65살까지 코딩 2022. 1. 20. 09:51
728x90
반응형
import java.io.*;
import java.util.ArrayList;
import java.util.List;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//String 하나를 입력받고
        String s = br.readLine();
        //소대문자 구분없이 세고 출력이 대문자기 때문에 대문자로 바꿔줌
        s = s.toUpperCase();
        char[] chars = s.toCharArray();
        int[] count = new int[s.length()];
        List<Character> list = new ArrayList<>();
		//String을 char배열로 바꾸어 반복문을 돌면서 몇개인지 카운트
        for(int i =0; i <chars.length-1; i++) {
        //이미 센 알파벳이라면 세지 않음
            if(!list.contains(chars[i])) {
                for (int j = i+1; j < chars.length; j++) {
                    if (chars[i] == chars[j]) {
                        count[i]++;
                    }
                }
            }
            //센 알파벳을 저장
            list.add(chars[i]);

        }
        int max = 0;
        int tmp =0;
        // count의 맥스값과 index값 찾기
        for(int i =0; i<count.length; i++){
            if(count[i] > max){
                max = count[i];
                tmp = i;
            }
        }
		//max가 tmp가 아니고 다른곳에서 같다면 max값이 2이상이므로 ?출력
        for(int i =0; i<count.length; i++){
            if(count[i] ==max && i !=tmp){
                System.out.println("?");
                return;
            }
        }
        //그렇지 않다면 max값의 인덱스인 tmp에 있는 알파벳 출력
        System.out.println(chars[tmp]);


    }


}
728x90
반응형

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

[JAVA] 백준 달팽이는 올라가고 싶다 2869  (0) 2022.03.02
[JAVA] 백준 ACM호텔 10250  (0) 2022.03.02
백준 2941 크로아티아 알파벳 자바  (0) 2022.01.20
백준 4673 셀프넘버  (0) 2022.01.19
백준 4344 평균은 넘겠지  (0) 2022.01.19