[ 프로그래머스 ] #42888 : 오픈채팅방 - JAVA

🔗 오픈채팅방

문제 풀이

  • 최종으로 구하고자 하는 것 ➡️ 최종으로 보는 메시지
  • 입력 값 중 수정되지 않는 것 ➡️ 유저 아이디
  • 입력 값 중 수정 되는 것 ➡️ 닉네임
    • 수정되면 영향 받는 것 ➡️ 오픈 채팅방의 내용 변경
    • 수정 되는 조건 ➡️ Enter, Change 인 경우
1. userId, userName을 저장한다.
2. Enter, Leave 일 경우 메시지를 저장한다.
import java.util.*;
class Solution {

    // userId,userName
    static Map<String,String> uid=new HashMap<>();

    // command, message
    static Map<String, String> command=new HashMap<>();

    // messages
    static List<String> result=new ArrayList<>();

    public String[] solution(String[] record) {

        // 들어올 때 메시지 출력
        // 나갈 때 메시지 출력

        // 변경 방법
        // 1. 채팅방 나간 후, 새로운 닉네임으로 다시 들어옴
        // 2. 채팅방에서 닉네임 변경

        // 변경하면 기존 채팅 메시지의 닉네임도 모두 변경
        // 중복 닉네임 허용

        // 최종적인 메시지 return

        command.put("Enter","님이 들어왔습니다.");
        command.put("Leave","님이 나갔습니다.");

        // 모든 명령 순회
        // uid 업데이트
        for(int i=0;i<record.length;i++){
            String [] r=record[i].split(" ");

            if(r.length==3){// Enter.Change 일 경우
                // uid 업데이트
                uid.put(r[1],r[2]);
            }
        }

        // 메시지 업데이트
        for(int i=0;i<record.length;i++){
            String [] r=record[i].split(" ");
            if(command.containsKey(r[0])){
                result.add(
                    uid.get(r[1])+command.get(r[0])
                );
            }

        }

        return result.stream().toArray(String []:: new);
    }

}