Skip to content

Commit e07e506

Browse files
authored
Create Valid Parentheses.py
1 parent a5816c0 commit e07e506

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
3+
Problem 20 | Valid Parentheses
4+
https://leetcode.com/problems/valid-parentheses/
5+
6+
'''
7+
8+
class Solution:
9+
def isValid(self, s: str) -> bool:
10+
stack = []
11+
for i in s:
12+
if i in "({[":
13+
stack.append(i)
14+
elif len(stack):
15+
if stack[-1] == "(" and i == ")":
16+
stack.pop(-1)
17+
elif stack[-1] == "{" and i == "}":
18+
stack.pop(-1)
19+
elif stack[-1] == "[" and i == "]":
20+
stack.pop(-1)
21+
else:
22+
return False
23+
else:
24+
return False
25+
if len(stack):
26+
return False
27+
else:
28+
return True
29+
30+

0 commit comments

Comments
 (0)