Created
February 1, 2022 08:13
-
-
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: …
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: | |
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