From b564fc563ce37aec3e11be32970347168c7b42bb Mon Sep 17 00:00:00 2001 From: sangge-rockpi <2251250136@qq.com> Date: Sat, 20 Jan 2024 13:43:21 +0800 Subject: [PATCH] add leetcode 20 --- leetcode/20.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 leetcode/20.py diff --git a/leetcode/20.py b/leetcode/20.py new file mode 100644 index 0000000..e4eb84d --- /dev/null +++ b/leetcode/20.py @@ -0,0 +1,26 @@ +# give a string s containing just '(', ')' and else +# determine if the input is valid + + +class Solution: + def isValid(self, s: str) -> bool: + symbol_dict = {"(": ")", "{": "}", "[": "]"} + stack = [] + for i in s: + try: + stack_top = stack.pop() + except IndexError: + continue + + try: + if symbol_dict[stack_top] == i: + continue + except KeyError: + return False + + stack.append(i) + + if stack: + return False + + return True