public String[] solution(String[] record) {
List<String> chatLog = new ArrayList<>();
Map<String, String> nickMap = new HashMap<>();
for (String log : record) {
StringTokenizer st = new StringTokenizer(log);
String command = st.nextToken();
String userId = st.nextToken();
if (command.equals("Enter") || command.equals("Change")) {
String nickname = st.nextToken();
nickMap.put(userId, nickname);
}
if (command.equals("Enter")) {
chatLog.add(userId + "님이 들어왔습니다.");
} else if (command.equals("Leave")) {
chatLog.add(userId + "님이 나갔습니다.");
}
}
String[] answer = new String[chatLog.size()];
int logIdx = 0;
for (String str : chatLog) {
int endOfId = str.indexOf("님");
String userId = str.substring(0, endOfId);
answer[logIdx++] = str.replace(userId, nickMap.get(userId));
}
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/42888