DEV Community

Hector Williams
Hector Williams

Posted on Edited on

LeetCode #32. Longest Valid Parentheses

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;    
    }
}
Enter fullscreen mode Exit fullscreen mode

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;   
};
Enter fullscreen mode Exit fullscreen mode

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;    
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)