상세 컨텐츠

본문 제목

오픈채팅방 (2019 KAKAO BLIND RECRUITMENT)

프로그래머스 코딩테스트 풀이

by 발발개발 2022. 6. 30. 13:15

본문

원본 : https://programmers.co.kr/learn/courses/30/lessons/42888

 

코딩테스트 연습 - 오픈채팅방

오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오

programmers.co.kr

 

풀이

import java.util.*;

class Solution {
    public String[] solution(String[] record) {
        Map<String, String> user = new HashMap<>();
        int cnt = 0;

        for (String str : record) {
            String[] strSplit = str.split(" ");
            if ("Enter".equals(strSplit[0])) {
                user.put(strSplit[1], strSplit[2]);
                cnt++;
            } else if ("Change".equals(strSplit[0])) {
                user.replace(strSplit[1], strSplit[2]);
            } else {
                cnt++;
            }
        }

        String[] answer = new String[cnt];
        int idx = 0;

        for (String str : record) {
            String[] strSplit = str.split(" ");
            if ("Enter".equals(strSplit[0])) {
                answer[idx++] = user.get(strSplit[1]) + "님이 들어왔습니다.";
            } else if ("Leave".equals(strSplit[0])) {
                answer[idx++] = user.get(strSplit[1]) + "님이 나갔습니다.";
            }
        }

        return answer;
    }
}

관련글 더보기

댓글 영역