Given a string containing only '(' and ')', return the length of the longest valid (well-formed) parentheses substring.
- Use a stack to store indices.
- Initialize the stack with -1.
- Push indices of '('.
- For ')', pop from the stack.
- If the stack becomes empty, push the current index.
- Otherwise, update the maximum valid length.
O(n)
O(n)
Java