27 lines
634 B
Python
27 lines
634 B
Python
def isValid(s):
|
|
"""
|
|
:type s: str
|
|
:rtype: bool
|
|
"""
|
|
stack = [] # only use append and pop
|
|
pairs = {
|
|
'(': ')',
|
|
'{': '}',
|
|
'[': ']'
|
|
}
|
|
for bracket in s:
|
|
if bracket in pairs:
|
|
stack.append(bracket)
|
|
elif len(stack) == 0 or bracket != pairs[stack.pop()]: # Checks for corresponding dictionary value based on previous closing bracket
|
|
return False
|
|
print(stack)
|
|
return len(stack) == 0
|
|
|
|
|
|
|
|
# print(isValid("()")) # true
|
|
print(isValid("()[]{}")) # true
|
|
print(isValid("(]")) # false
|
|
print(isValid("([)]")) # false
|
|
print(isValid("{[]}")) # true
|