Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Created November 11, 2022 18:00
Show Gist options
  • Save trafficinc/0d9eef3d0b3faa489e8702d8fbdffc07 to your computer and use it in GitHub Desktop.
Save trafficinc/0d9eef3d0b3faa489e8702d8fbdffc07 to your computer and use it in GitHub Desktop.
Leetcode: Valid Parentheses (Python)
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