Algorithm
[프로그래머스] 오픈채팅방(2019 KAKAO BLIND RECRUITMENT)
HONGNEW
2021. 1. 9. 12:22
2019 카카오 코테 문제였던 오픈채팅방 문제를 풀어보았습니다.
오픈채팅방에 유저가 들어오고 나간 이력을 이용해, 닉네임 변경이 일어나면 이를 반영해서 메시지를 출력하는 문제입니다.
유저 아이디와 닉네임을 쌍으로 관리한다는 점에서 HashMap 자료구조를 이용해서 쉽게 해결할 수 있었습니다.
import java.util.*;
class Solution {
public String[] solution(String[] record) {
ArrayList<String> result = new <String> ArrayList();
HashMap<String, String> hm = new <String, String> HashMap();
for(int i=0; i<record.length; i++){
String[] array = record[i].split(" ");
if(!array[0].equals("Leave")){
hm.put(array[1], array[2]);
}
}
for(int i=0; i<record.length; i++){
String[] array = record[i].split(" ");
if(array[0].equals("Enter")){
result.add(hm.get(array[1])+"님이 들어왔습니다.");
}else if(array[0].equals("Leave")){
result.add(hm.get(array[1])+"님이 나갔습니다.");
}
}
String[] answer = new String[result.size()];
for(int i=0; i<result.size(); i++){
answer[i] = result.get(i);
}
return answer;
}
}