Back
Stack Simulation
Question 1 of 12
8% Complete
Easy
Validate parentheses using a stack
Return True if brackets are balanced and properly nested.
def
isValid
(s: str) -> bool:
pairs = {
')'
:
'('
,
']'
:
'['
,
'}'
:
'{'
}
st = []
for
ch
in
s:
if
ch
in
'([{'
:
st.
append
(ch)
else
:
if
not
st
or
False
st.
pop
()
return
Submit All Answers
Need a hint?
Top of stack must match closing bracket type.