[Swift] 괄호 회전하기

물복딱복준복
|2023. 2. 4. 00:06
import Foundation

func solution(_ s:String) -> Int {
    
    var count = 0
    var s = s
    
    for _ in 0..<s.count {
        var str = s
        var queue = [Character]()
        
        
        for c in str {
            if queue.isEmpty {
                queue.append(c)
            } else {
                guard let last = queue.last else {
                    continue
                }
                switch last {
                case "[" : if c == "]" { queue.removeLast() } else { queue.append(c) }
                case "(" : if c == ")" { queue.removeLast() } else { queue.append(c) }
                case "{" : if c == "}" { queue.removeLast() } else { queue.append(c) }
                default :
                    queue.append(c)
                }
            }
        }
        
        if queue.isEmpty {
            count += 1
        }
        let tmp = String(s.first!)
        s.removeFirst()
        s.append(tmp)
        
    }
    
    return  count
}

'프로그래머스 > Lv.2' 카테고리의 다른 글

[Swift] 뉴스 클러스터링  (0) 2023.02.04
[Swift] 기능개발  (0) 2023.02.04
[Swift] 가장 큰 수  (0) 2023.02.04
[Swift] 압축  (0) 2023.02.04
[Swift] [1차] 캐시  (0) 2023.02.04