Created
May 8, 2020 00:46
-
-
Save ZhouYang1993/59e94630a7391c545a2bb0a4d852470d to your computer and use it in GitHub Desktop.
Algorithms for Interview 1: Stack
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
| class Solution: | |
| def isValid(self, s: str) -> bool: | |
| mapping = {')': '(', ']': '[', '}': '{'} | |
| stack = [] | |
| for char in s: | |
| if mapping.get(char): | |
| if not stack: | |
| return False | |
| else: | |
| if stack[-1] == mapping[char]: | |
| stack.pop() | |
| else: | |
| 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