Skip to content

Commit b263e24

Browse files
committed
Add valid parentheses solution
1 parent af404b7 commit b263e24

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

valid-parentheses/hyunjung-choi.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)