Skip to content

Commit 16f36c0

Browse files
committed
Add solution 20
1 parent be68366 commit 16f36c0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

20_ValidParentheses.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func isValid(_ s: String) -> Bool {
3+
var stack = [Character]()
4+
for bracket in s {
5+
stack.append(bracket)
6+
if bracket == ")" || bracket == "}" || bracket == "]" {
7+
let right = stack.popLast()!
8+
if let left = stack.popLast() {
9+
if (left == "(" && right == ")") ||
10+
(left == "{" && right == "}") ||
11+
(left == "[" && right == "]") {
12+
continue
13+
} else {
14+
return false
15+
}
16+
} else {
17+
return false
18+
}
19+
}
20+
}
21+
if stack.isEmpty {
22+
return true
23+
} else {
24+
return false
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)