Skip to content

Commit ceda89c

Browse files
committed
[WEEK6](gmlwls96) valid parentheses
1 parent cb16f98 commit ceda89c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

valid-parentheses/gmlwls96.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ package leetcode_study
22

33
class Solution {
44
fun isValid(s: String): Boolean {
5-
5+
val stack = Stack<Char>()
6+
val openParentheses = "([{"
7+
s.forEach {
8+
if (openParentheses.contains(it)) {
9+
stack.push(it)
10+
} else {
11+
if (stack.isEmpty()) {
12+
return false
13+
}
14+
val top = stack.pop()
15+
if (
16+
top == openParentheses[0] && it != ')' ||
17+
top == openParentheses[1] && it != ']' ||
18+
top == openParentheses[2] && it != '}'
19+
) {
20+
return false
21+
}
22+
}
23+
}
24+
return stack.isEmpty()
625
}
7-
}
26+
}

0 commit comments

Comments
 (0)