From b6cd48650c3e7406c07e1a7082e1d954f5a55708 Mon Sep 17 00:00:00 2001 From: solairerove Date: Tue, 2 Jan 2024 14:17:18 +0800 Subject: [PATCH] feature: valid parentheses revisit --- stack/ValidParentheses.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stack/ValidParentheses.py b/stack/ValidParentheses.py index 49278ef..b4ffa1e 100644 --- a/stack/ValidParentheses.py +++ b/stack/ValidParentheses.py @@ -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)