LeetCode #32. Longest Valid Parentheses

●4 ●27 ●54
calendar_today ago • schedule1 min read
— Originally published at dev.to

Sometimes in leetcode style challenges,we need to use more than one tecnique.In the example below, we are using stacks combined with dynamic programming.

Problem
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 * 104
s[i] is '(', or ')'.

Discussion
We traverse the array twice. We use a stack to keep track of the matching opening and closing brackets and an array to track the length of valid sequences. We place the number for every opening bracket on the stack and every time we find a matching closing bracket we take off the matching opening bracket from the stack.
We add the length to the lengths array to get the length of that valid sequence.
On our second traversal, we check the value of the lengths.If it isn't zero, we add to the current length and compare the max length. On finishing the traveral, we return the max length.

Solution

Java

class Solution {
    public int longestValidParentheses(String s) {
     int [] lengths=new int[s.length()];
     int length=0;
     int maxLength=0;
     Stack<Integer> indexes=new Stack<>();
     for(int i=0;i<s.length();i++){
      char bracket=s.charAt(i);
      if(bracket=='(')indexes.push(i);
      else{
       if(!indexes.isEmpty()){
        int index=indexes.pop();
        lengths[index]=(i-index)+1;
       } 
      }   
     }
     int index=0;
     while(index<s.length()){
      char bracket=s.charAt(index);
      if(lengths[index]!=0){
       length+=lengths[index];
       maxLength=Math.max(maxLength,length);
       index+=lengths[index]; 
      }else{
        index++;
        length=0;
      } 
      }   
     return maxLength;    
    }
}

JavaScript

 var longestValidParentheses = function(s) {
 const lengths=[];
 lengths.length=s.length;
 let length=0;
 let maxLength=0;
 const indexes=[];
 for(let i=0;i<s.length;i++){
  var bracket=s.charAt(i);
  if(bracket=='('){
   indexes.push(i);
   lengths[i]=0;  
  }else{
   if(indexes.length!=0){
    let index=indexes.pop();
    lengths[index]=(i-index)+1;
   }
   lengths[i]=0; 
  }   
 }
 let index=0;
 while(index<s.length){
  var bracket=s.charAt(index);
  if(bracket=='('){
  if(lengths[index]==0){
   index++;
   length=0;
  }else{
    length+=lengths[index];
    maxLength=Math.max(length,maxLength);
    index+=lengths[index];
  }         
  }else{
    index++;
    length=0;
  } 
 }   
 return maxLength;   
};

C#

public class Solution {
    public int LongestValidParentheses(string s) {
     int [] lengths=new int[s.Length];
     int length=0;
     int maxLength=0;
     Stack<int> indexes=new Stack<int>();
     for(int i=0;i<s.Length;i++){
      char bracket=s[i];
      if(bracket=='(')indexes.Push(i);
      else{
       if(indexes.Count!=0){
        int index=indexes.Pop();
        lengths[index]=(i-index)+1;
       } 
      }   
     }
     int index1=0;
     while(index1<s.Length){
      char bracket=s[index1];
      if(lengths[index1]!=0){
       length+=lengths[index1];
       maxLength=Math.Max(maxLength,length);
       index1+=lengths[index1]; 
      }else{
        index1++;
        length=0;
      } 
      }   
     return maxLength;    
    }
}

Want more #algorithms & #dsa deep dives?

Join 6,134+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

BFS vs DFS? The Question That Fixed My Graph Solutions

prakhar_srv - May 22

FAANG Coding Interviews Aren’t the Same: The DSA Patterns Each Company Actually Tests

prakhar_srv - May 12

How to Know If You’re Ready for FAANG Interviews

prakhar_srv - May 9

300. Longest Increasing Subsequence

Hector Williams - Mar 18

LeetCode #3693. Climbing Stairs II

Hector Williams - Nov 24
chevron_left
1.5k Points • 85 Badges
25Posts
8Comments
30Connections
An experienced freelance software engineer from the Caribbean!!!

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!