Advertisement

Longest Valid Parentheses - LeetCode 32 Solution

Longest Valid Parentheses - Complete Solution Guide

Longest Valid Parentheses is LeetCode problem 32, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string containing just the characters '(' and ')' , return the length of the longest valid (well-formed) parentheses substring . Example 1: Input: s = "(()" Output: 2 Explanation: The longest valid parentheses substring is "()". Example 2: Input: s = ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()". Example 3: Input: s = "" Output: 0 Constraints: 0 <= s.length <= 3 * 10 4 s[i] is '(' , or ')' .

Detailed Explanation

The problem asks us to find the length of the longest substring within a given string that contains only '(' and ')' characters, where the substring forms a valid (well-formed) parentheses sequence. A valid parentheses sequence means that each opening parenthesis '(' has a corresponding closing parenthesis ')' to its right, and they are properly nested.

Solution Approach

The provided solution uses a stack to keep track of the indices of unmatched opening parentheses. The algorithm iterates through the string. If it encounters an opening parenthesis, it pushes its index onto the stack. If it encounters a closing parenthesis, it pops an element from the stack. If the stack becomes empty after the pop, it means we've closed all the previously opened parentheses, and the current closing parenthesis might be the start of a new valid substring. In this case, we push the index of the current closing parenthesis onto the stack. If the stack is not empty after the pop, it means there's still an unmatched opening parenthesis in the stack. We calculate the length of the current valid substring as the difference between the current index and the index at the top of the stack. We keep track of the maximum length encountered so far.

Step-by-Step Algorithm

  1. Step 1: Initialize a stack with -1 to represent the index before the string starts. This serves as a base for the initial valid substring.
  2. Step 2: Iterate through the input string `s` from left to right.
  3. Step 3: If the current character is an opening parenthesis '(', push its index onto the stack.
  4. Step 4: If the current character is a closing parenthesis ')', pop an element from the stack.
  5. Step 5: After popping, check if the stack is empty. If it is, it means the popped ')' closed the last valid sequence, so push the current index `i` onto the stack. This marks the start of a new potentially valid sequence after this ')'
  6. Step 6: If the stack is not empty after popping, calculate the length of the current valid substring by subtracting the index at the top of the stack from the current index `i` (current_len = i - stack.top()).
  7. Step 7: Update the `max_len` with the maximum value seen so far: max_len = max(max_len, current_len)
  8. Step 8: After iterating through the entire string, return `max_len`.

Key Insights

  • Insight 1: A stack can be used to keep track of the positions of unmatched opening parentheses. When a closing parenthesis is encountered, we can try to match it with an opening parenthesis from the stack.
  • Insight 2: The key is to maintain the index of the last unmatched ')' as the base of our current valid parenthesis string. This is achieved by initializing the stack with -1.
  • Insight 3: When the stack becomes empty after popping a ')' it means that the current ')' closes all the previously opened '(', and we need to push the index of this ')' onto the stack to mark a new base for subsequent valid substrings.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Dynamic Programming, Stack.

Companies

Asked at: Adobe, Amazon, Bloomberg, DE Shaw, InMobi, Intuit, MakeMyTrip, Meta, Microsoft, Oracle, SOTI, Salesforce, Sprinklr, TikTok, Uber, Yahoo, Zeta, Zoho, eBay.