Qustion
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
tips:
Stack:first in last out
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
d = {')':'(', '}':'{', ']':'['}
stack = []
for i in s:
if i in d and len(stack)!=0 and d[i]==stack[-1]:
stack.pop()
else:
stack.append(i)
return len(stack)==0