Given a string consisting of opening and closing parenthesis, find length of the longest valid parenthesis substring.

Examples:

Input : ((()
Output : 2
Explanation : ()

Input: )()())
Output : 4
Explanation: ()() 

Input:  ()(()))))
Output: 6
Explanation:  ()(()))
[ad type=”banner”]

We strongly recommend you to minimize your browser and try this yourself first.

A Simple Approach is to find all the substrings of given string. For every string, check if it is a valid string or not. If valid and length is more than maximum length so far, then update maximum length. We can check whether a substring is valid or not in linear time using a stack (See this for details). Time complexity of this solution is O(n2.

An Efficient Solution can solve this problem in O(n) time. The idea is to store indexes of previous starting brackets in a stack. The first element of stack is a special element that provides index before beginning of valid substring (base for next valid string).

1) Create an empty stack and push -1 to it. The first element
   of stack is used to provide base for next valid string. 

2) Initialize result as 0.

3) If the character is '(' i.e. str[i] == '('), push index 
   'i' to the stack. 
   
2) Else (if the character is ')')
   a) Pop an item from stack (Most of the time an opening bracket)
   b) If stack is not empty, then find length of current valid
      substring by taking difference between current index and
      top of the stack. If current length is more than result,
      then update the result.
   c) If stack is empty, push current index as base for next
      valid substring.

3) Return result.
[ad type=”banner”]

Below are C++  implementations of above algorithm.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20length%20of%20the%20longest%20valid%0A%2F%2F%20substring%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Aint%20findMaxLen(string%20str)%0A%7B%0A%20%20%20%20int%20n%20%3D%20str.length()%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20stack%20and%20push%20-1%20as%20initial%20index%20to%20it.%0A%20%20%20%20stack%3Cint%3E%20stk%3B%0A%20%20%20%20stk.push(-1)%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20result%0A%20%20%20%20int%20result%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20all%20characters%20of%20given%20string%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20opening%20bracket%2C%20push%20index%20of%20it%0A%20%20%20%20%20%20%20%20if%20(str%5Bi%5D%20%3D%3D%20′(‘)%0A%20%20%20%20%20%20%20%20%20%20stk.push(i)%3B%0A%20%0A%20%20%20%20%20%20%20%20else%20%2F%2F%20If%20closing%20bracket%2C%20i.e.%2Cstr%5Bi%5D%20%3D%20’)’%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Pop%20the%20previous%20opening%20bracket’s%20index%0A%20%20%20%20%20%20%20%20%20%20%20%20stk.pop()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20this%20length%20formed%20with%20base%20of%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20current%20valid%20substring%20is%20more%20than%20max%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20so%20far%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!stk.empty())%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20max(result%2C%20i%20-%20stk.top())%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20stack%20is%20empty.%20push%20current%20index%20as%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20base%20for%20next%20valid%20substring%20(if%20any)%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20stk.push(i)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20result%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20string%20str%20%3D%20%22((()()%22%3B%0A%20%20%20%20cout%20%3C%3C%20findMaxLen(str)%20%3C%3C%20endl%3B%0A%20%0A%20%20%20%20str%20%3D%20%22()(()))))%22%3B%0A%20%20%20%20cout%20%3C%3C%20findMaxLen(str)%20%3C%3C%20endl%20%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

4
6
[ad type=”banner”]

Explanation with example:

Input: str = "(()()"

Initialize result as 0 and stack with one item -1.

For i = 0, str[0] = '(', we push 0 in stack

For i = 1, str[1] = '(', we push 1 in stack

For i = 2, str[2] = ')', currently stack has [-1, 0, 1], we pop
from the stack and the stack now is [-1, 0] and length of current
valid substring becomes 2 (we get this 2 by subtracting stack top 
from current index).
Since current length is more than current result, we update result.

For i = 3, str[3] = '(', we push again, stack is [-1, 0, 3].

For i = 4, str[4] = ')', we pop from the stack, stack becomes 
[-1, 0] and length of current valid substring becomes 4 (we get 
this 4 by subtracting stack top from current index). 
Since current length is more than current result, we update result.