백준 & 프로그래머스
프로그래머스. 완주하지 못한 선수. Java and Python
concho
2024. 1. 26. 16:33
1. java
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
HashMap<String,Integer> hMap = new HashMap<String,Integer>();
for(String c :completion){
if(hMap.containsKey(c)){
hMap.put(c, hMap.get(c)+1);
}else{
hMap.put(c, 1);
}
}
for(String p : participant){
if(hMap.containsKey(p)){
int tm = hMap.get(p) - 1;
if(tm == -1) return p;
else hMap.put(p,tm);
}else return p;
}
return answer;
}
}
2. python
def solution(participant, completion):
answer = ''
hDict = dict();
for c in completion:
if c in hDict:
hDict[c] += 1
else:
hDict[c] = 1
for p in participant:
if p in hDict:
hDict[p] -= 1
if hDict[p] == -1: return p
else:
return p
return answer