Skip to content

Instantly share code, notes, and snippets.

@alphaCoder
Created September 12, 2019 09:35
Show Gist options
  • Save alphaCoder/d305fe4d622c34bebf52f8a7d8f95bbe to your computer and use it in GitHub Desktop.
Save alphaCoder/d305fe4d622c34bebf52f8a7d8f95bbe to your computer and use it in GitHub Desktop.
Valid Parenthesis Approach 2
class Solution:
def isValid(self, s: str) -> bool:
stack = []
bracketsMap = {"(": ")", "{": "}", "[": "]" }
for k in s:
if k in bracketsMap:
stack.append(k)
else:
if not stack:
return False
else:
top = stack.pop()
if (bracketsMap[top] != k):
return False
return len(stack) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment