Stage 1 · Code
Stacks & Queues
Parentheses Problems
Valid parentheses, longest valid, and score of parentheses.
Valid Parentheses
Check if string of brackets is valid. Stack: push opening, pop and match on closing. Empty at end = valid.
Longest Valid Parentheses
Find length of longest valid parentheses substring. Stack of indices: push -1 initially, push opening index, pop on closing. If stack empty after pop, push current index as new base.
Score of Parentheses
Score: () = 1, AB = A+B, (A) = 2*A. Stack tracks depth score. On '(', push 0. On ')', pop top v, push max(2*v, 1). Sum stack at end.
Generate Parentheses
Generate all valid combinations of n pairs. Backtracking: add '(' if count < n, add ')' if close < open.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.