Advertisement
FreyasSpirit

Untitled

Feb 17th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. class Solution(object):
  2. def isValid(self, s):
  3. """
  4. :type s: str
  5. :rtype: bool
  6. """
  7. stack = []
  8. for char in s:
  9. if char in {'(', '[', '{'}:
  10. stack.append(char)
  11. elif len(stack) == 0:
  12. return False
  13. elif char == ')':
  14. if stack[-1] == '(':
  15. stack = stack[:-1]
  16. else:
  17. return False
  18. elif char == ']':
  19. if stack[-1] == '[':
  20. stack = stack[:-1]
  21. else:
  22. return False
  23. elif char == '}':
  24. if stack[-1] == '{':
  25. stack = stack[:-1]
  26. else:
  27. return False
  28.  
  29. if len(stack) == 0:
  30. return True
  31. else:
  32. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement