๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Swift ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ 2019 kakao

by Jouureee 2022. 5. 2.

๋ฌธ์ œ :

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ

์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ ์นด์นด์˜คํ†ก ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ์—์„œ๋Š” ์นœ๊ตฌ๊ฐ€ ์•„๋‹Œ ์‚ฌ๋žŒ๋“ค๊ณผ ๋Œ€ํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ณธ๋ž˜ ๋‹‰๋„ค์ž„์ด ์•„๋‹Œ ๊ฐ€์ƒ์˜ ๋‹‰๋„ค์ž„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์‹ ์ž…์‚ฌ์›์ธ ๊น€ํฌ๋ฃจ๋Š” ์นด์นด์˜คํ†ก ์˜ค

programmers.co.kr

๋ถ„์„ :

ํ™•์‹คํžˆ swift๋Š” c++๋ณด๋‹ค ๋ฌธ์ž์—ด ํŒŒ์‹ฑ์— ์žˆ์–ด์„œ ์œ ์šฉํ•œ๊ฒƒ ๊ฐ™๋‹ค ! ๊ทธ๋ž˜์„œ ์•„์ด๋””์–ด๋งŒ ์ƒ๊ฐ๋‚˜๋ฉด ๊ตฌํ˜„ํ•˜๊ธฐ ์ˆ˜์›”ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์„ ๋‹ค์‹œํ•œ๋ฒˆ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. swift ์งฑ ..๐Ÿ‘

์ด ๋ฌธ์ œ๋Š” ๋ฌธ์ž์—ด ํŒŒ์‹ฑ ๋ฐ ๊ตฌํ˜„ ๋ฌธ์ œ๋กœ, ์˜คํ”ˆ ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด์™”์„๋•Œ, ๋‚˜๊ฐ”์„๋•Œ, ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ–ˆ์„๋•Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ธฐ์กด ํ†ก๋ฐฉ ์ฐธ์—ฌ์ž๊ฐ€ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ  ๋‹ค์‹œ ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ํ†ก๋ฐฉ์—๋Š” ์ „ ์ด๋ฆ„ -> ํ˜„์žฌ ์ด๋ฆ„์œผ๋กœ ๊ฐ™์ด ๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค. 

๋”ฐ๋ผ์„œ ์ฒ˜์Œ์—” for๋ฌธ์„ ๋Œ๋ฉด์„œ enter์™€ change ๋ช…๋ น์–ด ์‹œ, inputDictionary์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ ์ €๋ผ๋ฉด ์ถœ๋ ฅ ๋ฐฐ์—ด์„ ๋Œ๋ฉฐ ํ•˜๋‚˜์”ฉ

replacingOccurrences(of:\(์˜ˆ์ „ ์ด๋ฆ„), with: \(ํ˜„์žฌ ์ด๋ฆ„))์œผ๋กœ ๋Œ€์ฒดํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ 25๋ถ€ํ„ฐ ์ฃผ๋ฅด๋ฅต ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค... 

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด ์•„์ด๋””์–ด๋ฅผ ์–ป์—ˆ๋‹ค.

for๋ฌธ์„ ๋Œ๊ธฐ ์ „์— inputDictionary์— ๋งˆ์ง€๋ง‰ ์ด๋ฆ„์„ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ฒŒ ํ•œ ๋’ค, ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด enter, change์‹œ ์ถœ๋ ฅ๋ฌธ ๋‚ด ๊ฐ™์€ ์•„์ด๋”” ๊ฐ€์ง„ ์‚ฌ๋žŒ์„ ์ฐพ์•„ ๋Œ€์ฒดํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์  ..!

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ํ•ด๊ฒฐ ๋˜์—ˆ๋‹ค. ํ•ด์‹œ ๋งต ๋ฐฉ์‹์ด for๋ฌธ๋ณด๋‹ค ๋น ๋ฆ„์„ ๋Š๋‚„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

Swift ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/05/02.
//

import Foundation
var inputDic:[String: String] = [:]
var answer = [(String, String)]()

func reName(_ id: String, _ name: String) {
    if inputDic.keys.contains(id) { // id๊ฐ€ ์ €์žฅ๋œ ์‚ฌ๋žŒ์ด๋ผ๋ฉด
        for (idx, i) in answer.enumerated() {
            if i.1 == id {
                answer[idx].0 = i.0.replacingOccurrences(of: "\(inputDic[i.1]!)", with: "\(name)")
            }
        }
    }
    inputDic[id] = name
    
}

func solution(_ record:[String]) -> [String]{
    for line in record {
        let l = line.components(separatedBy: " ")
        let command = l[0]
        let id = l[1]

        switch command {
        case "Enter":
            let name = l[2]
            reName(id, name)
            answer.append(("\(name)๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.", id))
        case "Leave":
            
            answer.append(("\(inputDic[id]!)๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.", id))
        case "Change":
            let name = l[2]
            reName(id, name)
        default:
            print("")
        }
    }
    return answer.map { $0.0 }
}

 

Swift ํ•ด๊ฒฐ ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/05/02.
//

import Foundation

func solution(_ record:[String]) -> [String]{
    var inputDic:[String: String] = [:]
    var answer = [String]()
    for line in record {
        let l = line.components(separatedBy: " ")
        if l.count > 2 {
            inputDic[l[1]] = l[2]
        }
    }
    
    for line in record {
        let l = line.components(separatedBy: " ")
        let command = l[0]
        let id = l[1]
        
        switch command {
        case "Enter":
            answer.append("\(inputDic[id]!)๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.")
        case "Leave":
            answer.append("\(inputDic[id]!)๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.")
        default:
            print("")
        }
    }
    return answer
}

๊ฒฐ๊ณผ :

๋Œ“๊ธ€