def isValid(s) :
stack = []
for i in s:
t= True
if i=="(" or i=="{" or i=="[":
stack.append(i)
elif i==')' and stack[-1]=="(":
stack.pop()
elif i=='}' and stack[-1]=="{":
stack.pop()
elif i==']' and stack[-1]=="[":
stack.pop()
else:
t= False
return t
print(isValid("()"))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter