Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created September 11, 2022 19:52
Show Gist options
  • Save ZhouYang1993/61a8e9bcbeb6e9b16e1b552e4da3daf1 to your computer and use it in GitHub Desktop.
Save ZhouYang1993/61a8e9bcbeb6e9b16e1b552e4da3daf1 to your computer and use it in GitHub Desktop.
deque in Python
from collections import deque
class Solution:
def isValid(self, s: str) -> bool:
mapping = {')': '(', ']': '[', '}': '{'}
stack = deque()
for char in s:
if mapping.get(char):
if len(stack) == 0:
return False
else:
if stack.pop() != mapping[char]:
return False
else:
stack.append(char)
return not stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment