Skip to content

Commit a64d499

Browse files
committed
valid-parentheses
1 parent 2603248 commit a64d499

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

valid-parentheses/socow.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
4+
if len(s) % 2 ==1:
5+
return False
6+
7+
pair = {")":"(","}":"{","]":"["}
8+
all = set(pair.values())
9+
stack = []
10+
11+
for x in s:
12+
if x in all:
13+
stack.append(x)
14+
else:
15+
if not stack or stack[-1] !=pair[x]:
16+
return False
17+
stack.pop()
18+
return not stack

0 commit comments

Comments
 (0)