결국 이 문제의 핵심은, 해당 유저의 id를 기반으로, 유저의 최종 닉네임이 무엇이었냐?를 구해야 한다.
유저의 최대 수는 10만이고, Map 자료구조를 사용했다.
유저가 들어오거나, 닉넴 변경시마다 맵 자료구조에 해당 유저의 최종 닉네임을 기억한다.
이후, 다시 record 배열을 순회하며 유저의 id를 기반으로 닉넴을 맵 자료구조에서 찾아 다시 넣어준다.
import java.util.*;
class Solution {
public String[] solution(String[] record) {
HashMap<String, String> map = new HashMap();
int N = 0;
StringTokenizer st;
for(int i=0; i<record.length; i++) {
st = new StringTokenizer(record[i]);
String type = st.nextToken();
if (type.equals("Enter")) N++;
if (type.equals("Leave")) {
N++;
continue;
}
String id = st.nextToken();
String nickname = st.nextToken();
map.put(id, nickname);
}
String[] answer = new String[N];
StringBuilder sb;
int j = 0;
for(int i=0; i<record.length; i++) {
st = new StringTokenizer(record[i]);
String type = st.nextToken();
String id = st.nextToken();
String nickname = map.getOrDefault(id, "");
sb = new StringBuilder();
if (type.equals("Enter")) {
sb.append(nickname).append("님이 들어왔습니다.");
} else if (type.equals("Leave")) {
sb.append(nickname).append("님이 나갔습니다.");
} else {
continue;
}
answer[j++] = sb.toString();
}
return answer;
}
}
