Back to Month
HARD 03 Oct 2026 View on LeetCode

32. Longest Valid Parentheses

</> Solution

class Solution {
    public int longestValidParentheses(String s) {
        int n = s.length();
        int[] stack = new int[n + 1];
        int top = -1;
        int max = 0;
        stack[++top] = -1;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '(') {
                stack[++top] = i;
            } else {
                top--;
                if (top < 0) {
                    stack[++top] = i;
                } else {
                    max = Math.max(max, i - stack[top]);
                }
            }
        }
        return max;
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(n)

TOPICS

Dynamic Programming Stack String