Longest Valid Parentheses Solution for Accenture

Company: Accenture · Difficulty: Medium · Topics: String, Stack, Dynamic Programming

Problem Statement

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

Key Understanding:

Examples

Example 1:

Input: s = "(()"

Output: 2

Explanation:

Example 2:

Input: s = ")()())"

Output: 4

Explanation:

Example 3:

Input: s = ""

Output: 0

Explanation:

Example 4:

Input: s = "((()"

Output: 2

Explanation:

Example 5:

Input: s = "()(()"

Output: 2

Explanation:

Intuition

The key insight is that valid parentheses must be balanced and consecutive. We can use a stack to keep track of indices of opening parentheses and calculate the length of valid substrings.

Core Concept:

Why Stack Works:

Mathematical Insight:

Approach

Approach 1: Stack (Optimal)

Algorithm:

1. Initialize stack with -1 to handle edge cases

2. For each character in the string:

3. Keep track of maximum length found

Why This Works:

Complexity Analysis:

Approach 2: Dynamic Programming

Algorithm:

1. Create DP array where dp[i] represents length of valid parentheses ending at i

2. For each ')', check if there's a matching '('

3. Update DP array and track maximum

Why This Works:

Complexity Analysis:

Approach 3: Two Pass Approach

Algorithm:

1. Scan from left to right counting '(' and ')'

2. Scan from right to left counting '(' and ')'

3. Track maximum valid length

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 2: s = ")()())"

Stack Approach:

Step 1: Initialize

Step 2: Process each character

Step 3: Return maxLength = 4

Dynamic Programming Approach:

Step 1: Initialize DP array

Step 2: Process each character

Step 3: Return max(dp) = 4

Key Insights

1. Stack Property: Stack naturally handles parentheses matching

2. Index Tracking: We track indices to calculate substring lengths

3. Edge Case Handling: Initialize stack with -1 for proper length calculation

4. Consecutive Nature: Valid parentheses must be consecutive

5. Balanced Property: Equal number of '(' and ')' in valid substring

Edge Cases

1. Empty String: Return 0

2. Single Character: Return 0 (no valid parentheses)

3. Starting with ')': Handle with stack initialization

4. All '(': Return 0 (no closing parentheses)

5. All ')': Return 0 (no opening parentheses)

6. Nested Parentheses: Algorithm handles nested cases correctly

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Stack (Optimal)

Time Complexity: O(n) - We traverse the string once

Space Complexity: O(n) - Stack can store up to n elements

Advantages:

Disadvantages:

Solution 2: Dynamic Programming

Time Complexity: O(n) - We traverse the string once

Space Complexity: O(n) - DP array of size n

Advantages:

Disadvantages:

Solution 3: Two Pass Approach

Time Complexity: O(n) - We traverse the string twice

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

More Accenture coding questions