Skip to content

Instantly share code, notes, and snippets.

@tanmay27vats
Created February 1, 2022 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanmay27vats/4c0288d9600eb3df07eb12e1f5a88533 to your computer and use it in GitHub Desktop.
Save tanmay27vats/4c0288d9600eb3df07eb12e1f5a88533 to your computer and use it in GitHub Desktop.
[Python] Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: …
class Solution:
def isValid(self, s: str) -> bool:
s = s.replace(' ','')
if len(s)%2 != 0:
return False
dict = {'(' : ')', '[' : ']', '{' : '}'}
ar = []
for i in s:
if i in dict:
ar.append(i)
else:
if not ar:
return False
pop = ar.pop()
if i != dict[pop]:
return False
return ar == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment