Created
September 12, 2019 09:31
-
-
Save alphaCoder/77d7f30b36d2e4dabd86952c14cb4610 to your computer and use it in GitHub Desktop.
Valid Parenthesis
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: | |
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