스택을 통해 올바른 형태의 괄호인지 체크 + 문자열 이동을 통해 새로운 문자열 생성
import Foundation
func solution(_ s:String) -> Int {
var total = 0
for idx in 0..<s.count {
let first = s.index(s.startIndex, offsetBy: idx)
let firstString = s[s.startIndex..<first]
let secondString = s[first..<s.endIndex]
let targetString = secondString + firstString
if isRightBrachet(word: String(targetString)) {
print(targetString)
total += 1
}
}
return total
}
func isRightBrachet(word: String) -> Bool {
var stack = [Character]()
var brachetPair:[Character:Character] = [")" : "(", "]" : "[", "}" : "{"]
for letter in word {
if let pair = brachetPair[letter] {
if let last = stack.last {
if last == pair {
stack.removeLast()
} else {
stack.append(letter)
}
} else {
stack.append(letter)
}
} else {
stack.append(letter)
}
}
return stack.isEmpty
}