Valid Substring | Practice | GeeksforGeeks

Given a string S consisting only of opening and closing parenthesis 'ie '('  and ')', find out the length of the longest valid(well-formed) parentheses substring.
NOTE: Length of the smallest valid substring ( ) is 2.
Example 1:
Input: S = "(()("
Output: 2
Explanation: The longest valid 
substring is "()". Length = 2.

Example 2:
Input: S = "()(())("
Output: 6
Explanation: The longest valid 
substring is "()(())". Length = 6.

Your Task:  
You dont need to read input or print anything. Complete the function findMaxLen() which takes S as input parameter and returns the maxlength.

Expected Time Complexity:O(n)
Expected Auxiliary Space: O(1)   

Constraints:
1 <= |S| <= 105
 
+4
amonk6 days ago
it is a hard question on leetcode
0
velspace011 week ago
Java sol TC-->O(N)
         SC-->O(1)
         static int findMaxLen(String s) {
      int max=0;
      int open=0;
      int close=0;
      for(int i=0;i<s.length();i++){
          if(s.charAt(i)=='('){
              open++;
          }else{
              close++;
          }
          if(open==close){
            max=Math.max(max,open+close);  
          }
          else if(close>open){
              open=0;
              close=0;
          }
      }
      close=0;
      open=0;
      for(int i=s.length()-1;i>=0;i--){
          if(s.charAt(i)=='('){
              open++;
          }
          else{
              close++;
          }
          if(open==close){
            max=Math.max(max,open+close);  
          }
          else if(open>close){
              open=0;
              close=0;
          }
      }
      return max;
    }
0
harshscode2 weeks ago
stack<int> st;      int res=0;        int n=s.length();         st.push(-1);      for(int i=0;i<n;i++)      {          if(s[i]=='(')          {             st.push(i);          }          else          {              st.pop();                            if(!st.empty())              {                  res=max(res,i-st.top());              }              else              {                  st.push(i);              }          }                          }      return res;      
-2
yadavbanti4953 weeks ago
int findMaxLen(string s) {       stack<int>S;       S.push(-1);       int count=0;       for(int i=0;i<s.length();i++)       {           if(s[i]=='(')           {               S.push(s[i]);           }           else if(s[i]==')' && S.top()!=-1)           {               if(S.top()=='(')               {                   count=count+2;                   S.pop();               }               else                {                   S.push(s[i]);               }           }       }       return count;
 
 
 
why this code not pass all the test case pls anyone find error
0
vishalja77191 month ago
  int findMaxLen(string s) {
   stack<int>st;
        int count=0;
        st.push(-1);
        for(int i=0;i<s.length();i++)
        {
            if(s[i] == '(')
            {
                st.push(i);
            }else if(s[i] == ')')
            {
                st.pop();
                if(!st.empty())
                {
                    count=max(count,i-st.top());
                }else{
                    st.push(i);
                }
            }
        }
        return count;
        
    }
+1
hanumanmanyam8371 month ago
class Solution {
    static int findMaxLen(String S) {
        // code here
        Stack<Integer>st=new Stack<>();
        int count=0;
        st.push(-1);
        for(int i=0;i<S.length();i++)
        {
            if(S.charAt(i)=='(')
            {
                st.push(i);
            }
            else if(S.charAt(i)==')')
            {
                st.pop();
                if(!st.isEmpty())
                {
                    count=Math.max(count,i-st.peek());
                    
                }
                else
                {
                    st.push(i);
                }
                
            }
            
        }
        return count;
    }
};
0
sanketgharatkar123
This comment was deleted.
0
aahankatiyar1 month ago
why code is not passing all test cases ??
 
   int findMaxLen(string s)    {       stack<char> sta;       int count=0;              for(int i=0; i< s.size() ; i++)       {           if(sta.empty())           {               sta.push(s[i]);           }                      else if(s[i] =='(')           {               sta.push(s[i]);           }                      else if(s[i]==')')           {               if(sta.top() == '(')               {                   count +=2;                   sta.pop();               }               else                {                   sta.push(s[i]);               }           }       }              return count;
}
+2
manaskhare330s1 month ago
Is it a easy question, I don't think so. 
0
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class Solution {
    static int findMaxLen(String s) {
        // code here
        Stack<Integer> st = new Stack<>();
        for(int i=0;i<s.length();i++){
            if(st.isEmpty() || s.charAt(i)=='(') st.push(i);
            else if(s.charAt(st.peek())=='(') st.pop();
            else st.push(i);
        }
        int curr = s.length();
        int ans = 0;
        while(!st.isEmpty()){
            ans = Math.max(ans,curr-st.peek()-1);
            curr = st.pop();
        }
        return Math.max(ans,curr);
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.