Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isValid(self, string: str) -> bool:
- stack = []
- mapping = {
- "}" : "{",
- ")" : "(",
- "]" : "["
- }
- for s in string:
- if s in ["{", "(", "["]:
- stack.append(s)
- elif s in ["}", ")", "]"]:
- if len(stack) > 0 and stack[-1] == mapping[s]:
- stack.pop()
- else:
- return False
- else:
- continue
- # return only if stack is empty
- return (len(stack) == 0)
Advertisement
Add Comment
Please, Sign In to add comment