Algorithm & Data Structures/Programers
Lv 2. 오픈채팅방
Geisha
2024. 7. 18. 23:50
코드 흐름
- 나가고 들어오고의 순서를 list에 저장하되 userId 기준으로 저장한다.
- map에는 닉네임의 변화를 key값을 id로 하여 저장한다.
- 모든 기록들을 " " 스페이스바 기준으로 split 하여 Enter 관련인지 Change관련인지 Leave관련인지 분류한다
- Enter시에는 list와 map에 최신화
- Leave시에는 list만 최신화
- Change 시에는 map만 최신화
- 이후 list를 순회돌면서 map의 최종 닉네임 값을 찾아 변경해준다.
import java.util.*;
class p오픈채팅방 {
public String[] solution(String[] record) {
ArrayList<String> list = new ArrayList<>();
HashMap<String,String> map = new HashMap<>();
for(String str : record){
String[] three = str.split(" ");
if(three[0].equals("Enter")){
list.add(three[1]+" 들어왔습니다.");
map.put(three[1],three[2]);
}
else if(three[0].equals("Leave"))
list.add(three[1]+" 나갔습니다.");
else
map.put(three[1],three[2]);
}
for(int i = 0 ; i < list.size() ; i++){
String s = list.get(i).split(" ")[0];
list.set(i,list.get(i).replaceAll(s,map.get(s)+"님이"));
}
String[] answer = new String[list.size()];
return list.toArray(answer);
}
}