Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1
- class Solution:
- def isValid(self, s: str) -> bool:
- stack = []
- mapping = {
- "(" : ")",
- "{" : "}",
- "[" : "]"
- }
- for character in s:
- if character in mapping:
- stack.append(character)
- elif character in list(mapping.values()):
- if len(stack)==0 or (mapping[stack.pop()] != character):
- return False
- if len(stack)>0:
- return False
- else:
- return True
- # 2
- 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)
- # 3
- # Didn't like the editorial. Skipping it.
Advertisement
Add Comment
Please, Sign In to add comment