728x90
코딩테스트 연습 - 오픈채팅방 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
import java.util.*;
class Solution {
public String[] solution(String[] record) {
HashMap <String, String> map =new HashMap<>();
int count=0;
for(int i=0;i<record.length;i++){
if(record[i].charAt(0)=='L') continue;
else if(record[i].charAt(0)=='C') count++; // 이름 교체 횟수
map.put(record[i].split(" ")[1],record[i].split(" ")[2]);
//HashMap에 userid(key) 와 이름(value)을 저장
//Ex. map(uid1234, Muzi)
}
String[] answer = new String[record.length-count];
int index=0;
for(int i=0;i<record.length;i++){
if(record[i].charAt(0)=='E'){
answer[index++]=map.get(record[i].split(" ")[1]) + "님이 들어왔습니다.";
}
else if(record[i].charAt(0)=='L'){
answer[index++]=map.get(record[i].split(" ")[1]) + "님이 나갔습니다.";
}
}
return answer;
}
}
728x90