-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.valid-parentheses.py
More file actions
35 lines (31 loc) · 862 Bytes
/
20.valid-parentheses.py
File metadata and controls
35 lines (31 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# @lc app=leetcode id=20 lang=python3
#
# [20] Valid Parentheses
#
from collections import deque
# @lc code=start
class Solution:
def corresponding(self, c: str):
if c == '(':
return ')'
if c == '[':
return ']'
if c == '{':
return '}'
def isValid(self, s: str) -> bool:
par_stack = deque()
for character in s:
if character in ['(', '[', '{']:
par_stack.append(character)
else:
if len(par_stack) == 0:
return False
stack_top = par_stack.pop()
if character != self.corresponding(stack_top):
return False
return False if len(par_stack) else True
# if __name__ == "__main__":
# s = Solution()
# print(s.isValid("(]"))
# @lc code=end