Skip to content

Commit 5dac1e3

Browse files
committed
20
1 parent 4039896 commit 5dac1e3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-parentheses/jeldo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# O(n), n = len(s)
3+
def isValid(self, s: str) -> bool:
4+
parentheses = {
5+
"(": ")",
6+
"]": "]",
7+
"{": "}",
8+
}
9+
stack = []
10+
for ch in s:
11+
if ch in parentheses.keys():
12+
stack.append(ch)
13+
elif stack and ch == parentheses[stack[-1]]:
14+
stack.pop()
15+
else:
16+
return False
17+
return len(stack) == 0

0 commit comments

Comments
 (0)