[Programmers / JAVA] Level 2 Open Chat Room (42888)
[Programmers / JAVA] Level 2 Open Chat Room (42888)
| Rank | Language Used |
|---|---|
| Level 2 | 🖼️ JAVA |
In KakaoTalk's open chat rooms, you can chat with people who aren't your friends, entering the chat room using a virtual nickname instead of your real one.
Kim Crew, a new employee, decided to build an admin screen for the person who created a KakaoTalk open chat room, so they can watch various people enter and leave. When someone enters the chat room, the following message is displayed.
- "[Nickname] has entered."
When someone leaves the chat room, the following message is displayed.
- "[Nickname] has left."
There are two ways to change your nickname in the chat room.
Leave the chat room, then re-enter with a new nickname.
Change your nickname while inside the chat room.
When you change your nickname, the nickname in all existing messages already displayed in the chat room is also changed.
For example, if people using the nicknames "Muzi" and "Prodo" enter the chat room in that order, the chat room displays the following messages.
- "Muzi has entered."
- "Prodo has entered."
If someone in the chat room leaves, the following message remains in the chat room.
- "Muzi has entered."
- "Prodo has entered."
- "Muzi has left."
If, after Muzi leaves, they re-enter under the nickname Prodo, then the existing "Muzi" entry remaining in the chat room is also changed to Prodo, as follows.
- "Prodo has entered."
- "Prodo has entered."
- "Prodo has left."
- "Prodo has entered."
Since the chat room allows duplicate nicknames, there are now two people using the nickname Prodo in the chat room. Now, if the Prodo who entered second changes their nickname to Ryan, the chat room messages change as follows.
- "Prodo has entered."
- "Ryan has entered."
- "Prodo has left."
- "Prodo has entered."
Given a string array record containing the history of entries, exits, and nickname changes in the chat room as a parameter, complete the solution function that returns, as a string array, the final messages seen by the person who created the room once all records have been processed.
- record is an array containing strings, with a length between 1 and 100,000, inclusive.
- Below is a description of the strings contained in record.
- Every user is identified by their [user ID].
- A user with [user ID] entering the chat room under the nickname [nickname] - "Enter [user ID] [nickname]" (ex. "Enter uid1234 Muzi")
- A user with [user ID] leaving the chat room - "Leave [user ID]" (ex. "Leave uid1234")
- A user with [user ID] changing their nickname to [nickname] - "Change [user ID] [nickname]" (ex. "Change uid1234 Muzi")
- The first word is one of Enter, Leave, or Change.
- Each word is separated by a space, and consists only of uppercase letters, lowercase letters, and digits.
- User IDs and nicknames are case-sensitive.
- The length of user IDs and nicknames is between 1 and 10, inclusive.
- Invalid input, such as a user who has left the chat room changing their nickname, will not be given.
| record | result |
|---|---|
| { "Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan" } | { "Prodo has entered.", "Ryan has entered.", "Prodo has left.", "Prodo has entered." } |
Example #1
Same as described in the problem.
In KakaoTalk's open chat room, depending on settings, you can freely set any nickname you want and change it at any time.
When you enter an open chat room, the nickname of the person who entered is displayed. Also, every time you send a message, your nickname and profile photo are displayed alongside it.
What happens if you change your nickname here? All profile info and nicknames prior to the change get updated to reflect it. That's exactly what we need to implement.
Since a nickname is highly mutable, data should be stored keyed on the unique ID. A Map-family data structure is well suited here. Store the ID and nickname, and implement the behavior for each command.
- Enter: store the ID and nickname
- Change: change the nickname of that ID
- Leave: no action
For Enter, store the ID and nickname in the Map. For Change, update the changed nickname in the Map.
Once all commands have been analyzed, we can obtain the finally changed nickname per ID.
Now we just need to print the message for each command in order. Store the messages in a List object, then return them as an array.
- Enter: print the open chat room entry notification message
- Change: no action
- Leave: print the open chat room exit notification message
The nickname matching an ID is obtained from the Map.
JAVA
import java.util.ArrayList; import java.util.HashMap; /** * Open Chat Room class * * @author RWB * @since 2021.12.13 Mon 23:00:15 */ class Solution { /** * Method that returns the answer * * @param record: [String[]] nickname change records * * @return [String[]] answer */ public String[] solution(String[] record) { ArrayList<String[]> list = new ArrayList<>(); HashMap<String, String> ids = new HashMap<>(); for (String text : record) { String[] texts = text.split(" "); switch (texts[0]) { case "Enter" -> { list.add(new String[] { texts[0], texts[1] }); // If the ID has already been recorded if (ids.containsKey(texts[1])) { ids.replace(texts[1], texts[2]); } // Otherwise else { ids.put(texts[1], texts[2]); } } case "Change" -> ids.replace(texts[1], texts[2]); case "Leave" -> list.add(new String[] { texts[0], texts[1] }); } } return list.stream() .map(strings -> new StringBuilder(ids.get(strings[1])).append(strings[0].equals("Enter") ? "님이 들어왔습니다." : "님이 나갔습니다.").toString()) .toArray(String[]::new); } }
