We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent af404b7 commit b263e24Copy full SHA for b263e24
valid-parentheses/hyunjung-choi.kt
@@ -0,0 +1,27 @@
1
+/**
2
+ * 시간 복잡도: O(n)
3
+ * 공간 복잡도: O(n)
4
+ */
5
+
6
+class Solution {
7
+ fun isValid(s: String): Boolean {
8
+ if (s.length % 2 == 1) return false
9
10
+ val stack = Stack<Char>()
11
+ val pairs = mapOf(')' to '(', ']' to '[', '}' to '{')
12
13
+ for (char in s) {
14
+ when {
15
+ char in pairs -> {
16
+ if (stack.isEmpty() || stack.pop() != pairs[char]) {
17
+ return false
18
+ }
19
20
21
+ else -> stack.push(char)
22
23
24
25
+ return stack.isEmpty()
26
27
+}
0 commit comments