nathanwailes

LeetCode 20 - Valid Parentheses - 2022.12.30 solution

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