-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Parenthesis.py
39 lines (36 loc) · 1.17 KB
/
Valid Parenthesis.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
# Method - 1
class Solution:
def isValid(self, s: str) -> bool:
map = {')': '(', '}': '{', ']': '['}
stack = []
for c in s:
if c not in map:
stack.append(c)
continue
if not stack or stack[-1] != map[c]:
return False
stack.pop()
if not stack: return True
# Method -2
# Naive Approach
# class Solution:
# def isValid(self, s: str) -> bool:
# stack = []
# for char in s:
# if char == '(' or char == '{' or char == '[':
# stack.append(char)
# else:
# if not stack:
# return False
# if char == ')' and stack[-1] == '(':
# stack.pop()
# elif char == '}' and stack[-1] == '{':
# stack.pop()
# elif char == ']' and stack[-1] == '[':
# stack.pop()
# else:
# return False
# return not stack
# # hashmap = {"(": ")", "{": "}", "[": "]"}
# # q = collection.deque()
# # for i in q: