이 문제는 사실 자바스크립트로 조금 풀다 정확성 테스트는 다 통과하고 효율성에서 통과하지 못했었다 (indexOf를 썼기 때문인듯)
어쨌든 자바로 이번엔 해시를 무조건 쓰려고 했다
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i=0;i<completion.length;i++) {
if(hm.get(completion[i]) == null) {
hm.put(completion[i], 1);
} else {
hm.put(completion[i], hm.get(completion[i])+1);
}
}
for(int a=0;a<participant.length;a++) {
if (hm.get(participant[a]) != null) {
if (hm.get(participant[a]) > 0) {
hm.put(participant[a], hm.get(participant[a])-1);
} else {
return participant[a];
}
} else {
return participant[a];
}
}
return answer;
}
}