add leetcode 20

This commit is contained in:
sangge-rockpi 2024-01-20 13:43:21 +08:00
parent fd3c25b7da
commit b564fc563c

26
leetcode/20.py Normal file
View File

@ -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