Skip to content

Instantly share code, notes, and snippets.

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