본문 바로가기
Problem Solving

[Swift] 프로그래머스 오픈채팅방(lv. 2)

by DuncanKim 2023. 4. 6.
728x90

[Swift] 프로그래머스 오픈채팅방(lv. 2)

 

1. 문제

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

2. 접근

 

 

 

3. 코드

import Foundation

func solution(_ record:[String]) -> [String] {
    var result = [[String]]()
    var userList: Dictionary = [String : String]()
    for i in record {
        let temp = i.split(separator: " ").compactMap({ String($0) })
        switch temp[0] {
        case "Enter":
            userList[temp[1]] = temp[2]
            result.append([temp[1], "님이 들어왔습니다."])
        case "Leave":
            result.append([temp[1], "님이 나갔습니다."])
        case "Change":
            userList[temp[1]] = temp[2]
        default:
            break
        }
    }
    for (index, i) in result.enumerated() {
        result[index][0] = userList[i[0]]!
    }
    return result.compactMap({ $0.joined() })
}
728x90

댓글