Created
September 11, 2022 19:52
-
-
Save ZhouYang1993/61a8e9bcbeb6e9b16e1b552e4da3daf1 to your computer and use it in GitHub Desktop.
deque in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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