Skip to content

Commit e45ab5c

Browse files
committed
Solution Valid parentheses
1 parent d17c24c commit e45ab5c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

valid-parentheses/doitduri.swift

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

0 commit comments

Comments
 (0)