알고리즘

프로그래머스 완주하지 못한 선수

pine tree root 2022. 1. 27. 22:41

import java.util.*;

class Solution {

    public class MarathonInfo {
        String[] participant;
        String[] completion;
        String[] noCompletion;

        public MarathonInfo(String[] participant, String[] completion) {
            this.participant = participant;
            this.completion = completion;
        }

        public void setNoCompletion(String[] noCompletion) {
            this.noCompletion = noCompletion;
        }
    }

    public String[] computeNoCompleInfo(MarathonInfo marathonInfo) {
        HashMap<String, Integer> computeInfo = new HashMap<>();

        for (String one : marathonInfo.participant) {
            computeInfo.put(one, computeInfo.getOrDefault(one, 0) + 1);
        }

        for (String comple : marathonInfo.completion) {
            computeInfo.put(comple, computeInfo.get(comple) - 1);
        }

        String[] gogo = computeInfo.entrySet().stream()
                .filter(part -> part.getValue() != 0)
                .map(tt -> tt.getKey())
                .toArray(String[]::new);

        return gogo.clone();
    }

    public String solution(String[] participant, String[] completion) {
        String answer = "";

        MarathonInfo marathonInfo = new MarathonInfo(participant, completion);

        marathonInfo.setNoCompletion(computeNoCompleInfo(marathonInfo));

        answer = marathonInfo.noCompletion[0];
        
        return answer;
    }
}