test.py 520 B

1234567891011121314151617181920212223
  1. class Solution:
  2. def isValid(self, s: str) -> bool:
  3. if len(s) <= 1:
  4. return False
  5. stack = []
  6. s_map = {
  7. ")": "(",
  8. "]": "[",
  9. "}": "{"
  10. }
  11. stack.append(s[0])
  12. for i in range(1, len(s)):
  13. if stack[-1] == s_map.get(s[i], None):
  14. stack.pop(-1)
  15. else:
  16. stack.append(s[i])
  17. return True if len(stack) == 0 else False
  18. solution = Solution()
  19. print(solution.isValid("{[]}"))