[Algorithm - Programmers] 완주하지 못한 선수

nunu·2023년 12월 17일
0

Algorithm

목록 보기
108/142

https://school.programmers.co.kr/learn/courses/30/lessons/42576

제출 코드

import java.util.HashMap;
class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";

        HashMap<String, Integer> hm = new HashMap<>();
        for (int i = 0; i < participant.length; i++) {
            if (hm.containsKey(participant[i])) {
                hm.replace(participant[i], hm.get(participant[i]) + 1);
            }
            else {
                hm.put(participant[i], 1);
            }
        }

        for (int i = 0; i < completion.length; i++) {
            int temp = hm.get(completion[i]);
            if (temp == 1) {
                hm.remove(completion[i]);
            }
            else {
                hm.replace(completion[i], temp - 1);
            }
        }

        for (String str : hm.keySet()) {
            answer += str;
        }
        return answer;
    }
}
profile
Hello, I'm nunu

0개의 댓글