Question DSA-STK-0063
HardWhat is the output of this code?
def longest_valid(s):
st, best = [-1], 0
for i, c in enumerate(s):
if c == "(":
st.append(i)
else:
st.pop()
if not st:
st.append(i)
else:
best = max(best, i - st[-1])
return best
print(longest_valid(")()())(()"), longest_valid("(()"))
- A.
4 2
- B.
2 2
- C.
Sign up free to see the answer and a step-by-step explanation.