[Swift 알고리즘] 오픈채팅방

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


import Foundation

func solution(_ record:[String]) -> [String] {
    
    var command = [[String]]()
    var dic = [String : String]()
    var answers = [String]()
    
    
    for i in record {
        let info = i.components(separatedBy: " ")
        command.append(info)
    }
    
    for i in command {
        let info = i[0]
        if info == "Enter" || info == "Change" {
            dic[i[1]] = i[2]
        }
    }
    
    for i in command {
        switch i[0] {
        case "Enter":
            let answer = "\(dic[i[1]]!)님이 들어왔습니다."
            answers.append(answer)
        case "Leave":
            let answer = "\(dic[i[1]]!)님이 나갔습니다."
            answers.append(answer)
        default:
            continue
        }
    }
    

    return answers
}

좋은 웹페이지 즐겨찾기