문제

문제출처 : Baekjoon

img

풀이

import java.util.*;

public class Main {
    static char solution(String str){

        str = str.toUpperCase();
        int[] arr = new int[26];

        for(int i = 0; i < str.length(); i++){
            char x = str.charAt(i);
            arr[x-65]++;
        }

        int max = 0;
        int result = 0;
        for(int j = 0; j < arr.length; j++){
            if(max < arr[j]){
                max = arr[j];
                result = j+65;
            }else if(max == arr[j]){
                result = '?';
            }
        }
        return (char)result;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        System.out.println(solution(str));
    }
}

회고

  1. for문을 어떻게 구조를 짜야할지 엄청 고민했던 문제. 처음에는 나눠서 안하고 for문안에 for문… 이런식으로 거의 4중for문까지 만들었었다ㅋㅋㅋ 뭔가 이상하더라!
  2. int -> char / char -> int 이렇게 아스키코드 이용해서 바꾸는게 아직도 어렵다. 아스키코드를 자유롭게 다루는건 많이 풀어봐야 늘것같다 ㅠㅠ