-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2504_괄호의값.swift
51 lines (46 loc) · 915 Bytes
/
2504_괄호의값.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Foundation
let input: [Character] = Array(readLine()!)
var stack: [Character] = []
var temp = 1
var answer = 0
for (i, c) in input.enumerated() {
switch c {
case "(":
stack.append(c)
temp *= 2
case "[":
stack.append(c)
temp *= 3
case ")":
guard let last = stack.last,
last == "("
else {
print(0)
exit(0)
}
if input[i-1] == "(" {
answer += temp
}
_ = stack.removeLast()
temp /= 2
case "]":
guard let last = stack.last,
last == "["
else {
print(0)
exit(0)
}
if input[i-1] == "[" {
answer += temp
}
_ = stack.removeLast()
temp /= 3
default:
break
}
}
if stack.isEmpty {
print(answer)
} else {
print(0)
}