nathanwailes

LeetCode 20 - Valid Parentheses - 2023.10.27 solution

Oct 27th, 2023
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. class Solution:
  2.     def isValid(self, s: str) -> bool:
  3.         """
  4.        """
  5.         stack = []
  6.         for c in s:
  7.             if c in ["(", "{", "["]:
  8.                 stack.append(c)
  9.             else:
  10.                 if len(stack) == 0:
  11.                     return False
  12.                 elif c == ")" and stack[-1] != "(":
  13.                     return False
  14.                 elif c == "}" and stack[-1] != "{":
  15.                     return False
  16.                 elif c == "]" and stack[-1] != "[":
  17.                     return False
  18.                 else:
  19.                     stack.pop()
  20.         return len(stack) == 0
  21.  
Advertisement
Add Comment
Please, Sign In to add comment