Created
September 12, 2019 09:35
-
-
Save alphaCoder/d305fe4d622c34bebf52f8a7d8f95bbe to your computer and use it in GitHub Desktop.
Valid Parenthesis Approach 2
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 = [] | |
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