Created
November 11, 2022 18:00
-
-
Save trafficinc/0d9eef3d0b3faa489e8702d8fbdffc07 to your computer and use it in GitHub Desktop.
Leetcode: Valid Parentheses (Python)
This file contains 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
def isValid(self, s): | |
""" | |
:type s: str | |
:rtype: bool | |
""" | |
if len(s) % 2 != 0: | |
return False | |
par_dict = {'(':')','{':'}','[':']'} | |
stack = [] | |
for char in s: | |
if char in par_dict.keys(): | |
stack.append(char) | |
else: | |
if stack == []: | |
return False | |
open_brac = stack.pop() | |
if char != par_dict[open_brac]: | |
return False | |
return stack == [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment