:type s: str :rtype: bool
(self, s)
| 39 | |
| 40 | class Solution(object): |
| 41 | def isValid(self, s): |
| 42 | """ |
| 43 | :type s: str |
| 44 | :rtype: bool |
| 45 | """ |
| 46 | left = "({[" |
| 47 | left_key = {')': '(', ']': '[', '}': '{'} |
| 48 | |
| 49 | stack = [] |
| 50 | |
| 51 | for i in s: |
| 52 | if i in left: |
| 53 | stack.append(i) |
| 54 | else: |
| 55 | try: |
| 56 | if stack[-1] == left_key[i]: |
| 57 | stack.pop() |
| 58 | else: |
| 59 | return False |
| 60 | except: |
| 61 | return False |
| 62 | |
| 63 | if stack: |
| 64 | return False |
| 65 | return True |