nathanwailes

LeetCode 20 - Valid Parentheses - NeetCode solution

Dec 30th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. class Solution:
  2.     def isValid(self, s: str) -> bool:
  3.         stack = []
  4.         closeToOpen = { ")" : "(", "]" : "[", "}" : "{" }
  5.  
  6.         for c in s:
  7.             if c in closeToOpen:
  8.                 if stack and stack[-1] == closeToOpen[c]:
  9.                     stack.pop()
  10.                 else:
  11.                     return False
  12.             else:
  13.                 stack.append(c)
  14.        
  15.         return True if not stack else False
Advertisement
Add Comment
Please, Sign In to add comment