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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Swift 2020 ์นด์นด์˜ค ๊ด„ํ˜ธ๋ณ€ํ™˜

by Jouureee 2022. 4. 14.

๋ฌธ์ œ :

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ด„ํ˜ธ ๋ณ€ํ™˜

์นด์นด์˜ค์— ์‹ ์ž… ๊ฐœ๋ฐœ์ž๋กœ ์ž…์‚ฌํ•œ "์ฝ˜"์€ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋กœ๋ถ€ํ„ฐ ๊ฐœ๋ฐœ์—ญ๋Ÿ‰ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋ฌธ์ œ์ ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ผ๋Š” ์—…๋ฌด ๊ณผ์ œ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์†Œ์Šค๋ฅผ

programmers.co.kr

๋‚œ์ด๋„ : level2

 

๋ถ„์„ : 

์ฃผ์–ด์ง„ ์š”๊ฑด์„ ์ž˜ ์ฝ๊ณ  ์š”๊ตฌ ์‚ฌํ•ญ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์ผ์ˆ˜๋ก ์„ค๊ณ„๋ฅผ ์ž˜ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋กœ์ง์ด ๊ผฌ์ด๊ธฐ ์‰ฌ์›Œ ์–ด๋ ค์šด๊ฑฐ ๊ฐ™๋‹ค(๋‚˜ํ•œํ…Œ๋งŒ ์ผ์ง€๋„ ๋ชจ๋ฅธ๋‹ค .. ใ…Ž) 

1,2,3,4๋ฒˆ์„ ์ฐจ๋ก€๋Œ€๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋˜ ํ•„์š”ํ•œ๊ฑด ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•ด ์ž‘์„ฑํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค ํŒ๋‹จ ๋“ค์—ˆ๋‹ค.

๋”ฐ๋ผ์„œ 1๋ฒˆ์€ isEmpty๋กœ ๊ฒ€์‚ฌํ•˜์˜€๋‹ค.

2๋ฒˆ์€ recursive ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋Œ๋ฉฐ "(" ์ˆ˜์™€ ")"์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๊ฐ™์„๋•Œ(๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด) u, ๊ทธ ์ดํ›„์˜ ๋ฌธ์ž๋ฅผ v๋กœ ๋ถ„๋ฆฌํ•˜์˜€๋‹ค.

3๋ฒˆ์€ check()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ "("์ผ ๊ฒฝ์šฐ stack์— ๋„ฃ๊ณ , ")"์ผ ๊ฒฝ์šฐ stack์— ์Œ“์ธ ๋ฌธ์ž๊ฐ€ "("์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ O(n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

4๋ฒˆ์€ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๊ฐ€ ์•„๋‹๊ฒฝ์šฐ ๋ถ„๊ธฐํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ด ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์—ˆ๋‹ค !! 

newString ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด "("๋ฅผ ๋ถ™์ธ ๋’ค 4-2๋ฒˆ์„ ์ˆ˜ํ–‰ํ• ๋•Œ v๋ฅผ ์žฌ๊ท€๋ฅผ ๊ฑฐ์ณ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ๋‹ค. ๊ทธ ๋’ค ")"๋ฅผ ๋ถ™์ด๊ณ  

4-4๋ฒˆ์ธ u์˜ ์ฒซ๋ฒˆ์งธ, ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ๋’ค u ์ŠคํŠธ๋ง์˜ ์›์†Œ๋ฅผ "(" -> ")", ")" -> "("๋กœ ๋ฐ”๊ฟ” newString ๋’ค์— ๋ฐ”๊พผ ์›์†Œ๋“ค์„ ๋ถ™์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

Swift ์ฝ”๋“œ :

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

import Foundation

func check(_ s: String) -> Bool {
    if s.isEmpty { return true }
    var s = s
    var stack = [String]()
    
    while !s.isEmpty {
        let x = String(s.removeFirst())
        if stack.isEmpty {
            stack.append(x)
        }else if stack.last! == "(" && x == ")" {
            stack.removeLast()
        }else {
            stack.append(x)
        }
    }
    return stack.isEmpty
}

func recursive(_ p: String) -> String {
    if p.isEmpty { return "" }
    else if check(p) {
        return p
    }
    
    var leftCnt = 0
    var rightCnt = 0
    var u = ""
    var v = ""
    for (idx, i) in p.enumerated() {
        if i == "(" {
            leftCnt += 1
            u += "("
        }else if i == ")" {
            rightCnt += 1
            u += ")"
        }
       
        if leftCnt == rightCnt {
            v = p.substring(from: idx+1, to: p.count-1)
            if(check(u)) { // ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€
                return u + recursive(v)
            }else { // ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฏ€๋กœ
                let newString = "(" + recursive(v) + ")"
                u.removeFirst()
                u.removeLast()
                //๋‚˜๋จธ์ง€ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ ๋’ค์ง‘๊ธฐ
                u = u.map {
                    if $0 == "(" { return ")" }
                    else { return "(" }
                }.reduce(""){$0 + $1}
                return newString + u

            }
        }
    }
    
    return ""
}

func solution(_ p:String) -> String {
    return recursive(p)
}

extension String {
    func substring(from: Int, to: Int) -> String {
        guard from < count, to >= 0, to - from >= 0 else {
            return ""
        }
        // Index ๊ฐ’ ํš๋“
        let startIndex = index(self.startIndex, offsetBy: from)
        let endIndex = index(self.startIndex, offsetBy: to + 1)
        
        // ํŒŒ์‹ฑ
        return String(self[startIndex ..< endIndex])
    }
}

๊ฒฐ๊ณผ :

๋Œ“๊ธ€