[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ

 

1. ๋ฌธ์ œ : Link

๋ฌธ์ž์—ด command / id / nickname์ด ๋“  ๋ฐฐ์—ด์ด input์œผ๋กœ ์ฃผ์–ด์ง€๊ณ , ํ•ด๋‹น ๋ฐฐ์—ด์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉด์„œ command์— ๋”ฐ๋ผ ์ฑ„ํŒ…์žฅ ์•Œ๋ฆผ์„ ๋ฟŒ๋ ค์ฃผ๋Š”๋ฐ ์ด๋•Œ ๋‹‰๋„ค์ž„์ด ๋ณ€๊ฒฝ๋  ์‹œ ๋ณ€๊ฒฝ๋œ ๋‹‰๋„ค์ž„์ด ์ด์ „ ๊ณต์ง€์— ๋ฐ˜์˜๋˜๊ฒŒ๋” ํ•˜๋Š” ์ฝ”๋“œ

 

2. ํ’€์ด

๋จผ์ € ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ ๋‹ด๊ณ  cmd์— ๋”ฐ๋ผ nickname์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

๊ทธ ๋‹ค์Œ ๊ณต์ง€์‚ฌํ•ญ ๋ฟŒ๋ คใ…ˆใ„ด๋‹ค.

 

3. ์ฝ”๋“œ

def solution(record):
    answer = []
    idic = {}
    for re in record:
        relist = re.split()
        cmd = relist[0]
        if cmd == 'Enter' or cmd == 'Change':
            idd = relist[1]
            nick = relist[2]
            idic[idd] = nick
    for re in record:
        relist = re.split()
        cmd = relist[0]
        if cmd == 'Enter':
            idd = relist[1]
            answer.append(idic[idd] + '๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.')
        elif cmd == 'Leave':
            idd = relist[1]
            answer.append(idic[idd] + '๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.')
    return answer
import java.util.*;

class Solution {
    public String[] solution(String[] record) {
        Map<String, String> hm = new HashMap<String, String>();
        
        for(String re : record){
            String[] reString = re.split(" ");
            String order = reString[0];
            String id = reString[1];
            if(order.equals("Enter")){
                String nick = reString[2];
                hm.put(id, nick);
            } else if(order.equals("Change")){
                String nick = reString[2];
                hm.put(id, nick);
            }
        }
        
        List<String> an = new ArrayList<String>();
        
        for(String re : record){
            String[] reString = re.split(" ");
            String order = reString[0];
            String id = reString[1];
            if(order.equals("Enter")){
                String nick = hm.get(id);
                String message = nick + "๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.";
                an.add(message);
            } else if(order.equals("Leave")){
                String nick = hm.get(id);
                String message = nick + "๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.";
                an.add(message);
            }
        }
        
        String[] answer = new String[an.size()];
        for(int i=0; i<an.size(); i++){
            answer[i] = an.get(i);
        }
        
        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ