-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_ValidParentheses.py
45 lines (37 loc) · 1.35 KB
/
20_ValidParentheses.py
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
36
37
38
39
40
41
42
43
44
45
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# Stack Dictionary
""" O(n) / O(n)
Valid input string
- (:) [:] {:} pair combination using dictionary
- key: closed / value: open
- when we see the closed, we should be able to match the open in order to validate
- it would be better to check from the back -> stack
i.e. ({[]}) - the sub expression of a valid expression should also be a valid expression
|]| what was on the top of the stack should be the match of incoming char
|[|
|{|
|(|
__
Corner case: what if there is single closed - temp.pop() runtime error
"""
valid = {}
valid[')'] = '('
valid['}'] = '{'
valid[']'] = '['
stack = []
for char in s:
if char in valid: # if closed
top = stack.pop() if stack else '!' # if there is stack, else assign dummy value - without value, stack.pop() runtime error
if valid[char] != top:
return False
else: # if open
stack.append(char)
return not stack # if not left in stack - true, else - false
if __name__ == '__main__':
s = ""
print(Solution().isValid(s))