Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZhouYang1993/59e94630a7391c545a2bb0a4d852470d to your computer and use it in GitHub Desktop.
Save ZhouYang1993/59e94630a7391c545a2bb0a4d852470d to your computer and use it in GitHub Desktop.
Algorithms for Interview 1: Stack
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