Skip to content

Latest commit

 

History

History
20 lines (14 loc) · 446 Bytes

File metadata and controls

20 lines (14 loc) · 446 Bytes

Longest Valid Parentheses - Java

Problem

Given a string containing only '(' and ')', return the length of the longest valid (well-formed) parentheses substring.

Approach

This solution uses a stack to store indices of parentheses. It scans the string once and calculates the maximum length of a valid parentheses substring.

Time Complexity

O(n)

Space Complexity

O(n)

How to Run

javac Solution.java
java Solution