def parenthesis(string):
openchr = {
'(' : 0,
'{' : 1,
'[' : 2
}
closechr = {
')' : 0,
'}' : 1,
']' : 2
}
visited = []
for char in string:
if char in openchr:
visited.append(char)
else:
if len(visited) == 0:
return False
popped = visited.pop(-1)
if (openchr[popped] != closechr[char]):
return False
if len(visited) == 0:
return True
return False
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