-
프로그래머스 - 오픈채팅방 [자바]Algorithms/- 프로그래머스 2022. 2. 4. 00:16
- 문제 링크 : 오픈채팅방
정답
import java.util.*; class Solution { public String[] solution(String[] record) { Map<String, String> ssMap = new HashMap<>(); int cnt = 0; for(String str : record){ if(str.startsWith("Enter") || str.startsWith("Change")){ String[] arr = str.split(" "); ssMap.put(arr[1], arr[2]); } if(str.startsWith("Change")){ cnt++; } } int len = record.length - cnt; String[] answer = new String[len]; int idx = 0; for(String str : record){ if(str.startsWith("Enter")){ String[] arr = str.split(" "); answer[idx] = ssMap.get(arr[1])+"님이 들어왔습니다."; idx++; }else if(str.startsWith("Leave")){ String[] arr = str.split(" "); answer[idx] = ssMap.get(arr[1])+"님이 나갔습니다."; idx++; } } return answer; } }
분석
- Map으로 아이디와 닉네임을 매핑하여 마지막에 answer에서 아이디 값으로 get하여 닉네임을 가져오면 된다.
- 어렵게 보이지만 단순한 문제
반응형