Skip to content

Commit

Permalink
feature: valid parentheses revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Jan 2, 2024
1 parent 58437cb commit b6cd486
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions stack/ValidParentheses.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# O(n) time || O(n) space
def is_valid(self, s: str) -> bool:
brackets = {')': '(', ']': '[', '}': '{'}
closed_to_open = {'}': '{', ']': '[', ')': '('}
stack = []
for br in s:
if br in brackets:
if not stack or stack.pop() != brackets[br]:
if br in closed_to_open:
if not stack or stack.pop() != closed_to_open[br]:
return False
else:
stack.append(br)
Expand Down

0 comments on commit b6cd486

Please sign in to comment.