백준 9046: 복호화

beomjun·2022년 4월 4일
0

BOJ

목록 보기
1/1

풀이

테스트 횟수를 입력받은후 반복문을 실행
빈공백도 입력가능하기때문에 replace 메소드를 통한 공백을 없앤후 charAt()메서드를 통해 'a'는0으로 계산하여 배열에 ++ 최대값 중복시 count 변수를 ++ 해준뒤 count가 1보다 클시 "?" 출력 아니라면 최다 알파벳 출력

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


class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        int num = Integer.parseInt(br.readLine()); //테스트횟수를 입력받는다.
        for (int h = 0; h < num; h++) {
            str = br.readLine().replace(" ", ""); // " "알파벳만 따지기때문에 replace메소드를 이용하여 빈공간제거
            int max = 0;  // 최다 알파벳 인덱스번호 저장
            int count = 0; // 최다 알파벳이 2개 이상일시 ++

            int[] arr = new int[26];
            for (int i = 0; i < str.length(); i++) {
                arr[str.charAt(i) - 'a']++;
            }
            for (int j = 1; j < arr.length; j++) {
                if (arr[max] < arr[j])
                    max = j;
            }
            for (int i = 0; i < arr.length; i++) {
                if (arr[max] == arr[i])
                    count++;

            }

            char ch = (char) (max + 'a');
            System.out.println(count > 1 ? "?" : ch);

        }
    }
}
profile
백엔드 개발자 지망생

0개의 댓글