본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 성격 유형 검사하기

by 현장 2026. 3. 13.

-Code

import java.util.*;

class Solution {
    static String[] types = {
        "RT", "CF", "JM", "AN"
    };
    
    public String solution(String[] survey, int[] choices) {
        // 테스트의 점수를 세기 위한 맵
        Map<Character, Integer> testCnt = new HashMap<>(Map.of(
            'R', 0, 'T', 0, 'C', 0, 'F', 0,
            'J', 0, 'M', 0, 'A', 0, 'N', 0
        ));
        // 각 입력을 기준으로 점수 계산
        for (int i = 0; i < survey.length; i++) {
            char left = survey[i].charAt(0);
            char right = survey[i].charAt(1);
            // 점수가 3 이하인 경우
            if (choices[i] <= 3) {
                testCnt.put(left, testCnt.get(left) + 4 - choices[i]);
            } else {
                //점수가 4이상인 경우
                // 4인 경우 계산을 안해야 하지만 -4로인해 0이됨
                testCnt.put(right, testCnt.get(right) + choices[i] - 4);
            }
        }
        // 결과 계산
        String answer = "";
        for (String type : types) {
            char left = type.charAt(0);
            char right = type.charAt(1);
            answer += testCnt.get(left) >= testCnt.get(right) ? left : right;
        }
        return answer;
    }
}