Skip to content

Commit 20baed8

Browse files
committed
20_valid-parentheses
1 parent 43d392f commit 20baed8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

20_valid-parentheses.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
stack = []
4+
parentheses_open = {
5+
"(": ")",
6+
"{": "}",
7+
"[": "]"
8+
}
9+
10+
parentheses_close = {
11+
")": "(",
12+
"}": "{",
13+
"]": "["
14+
}
15+
16+
for c in s:
17+
if parentheses_open.get(c) != None:
18+
stack.append(c)
19+
continue
20+
if parentheses_close.get(c) != None:
21+
if len(stack) > 0 and stack[-1] == parentheses_close.get(c):
22+
stack.pop()
23+
else:
24+
return False
25+
return True if len(stack) == 0 else False

0 commit comments

Comments
 (0)