human
Length of the longest valid substring | method to get length of the longest valid.
Create a stack and push - 1 as initial index to it ..
Initialize result.
Traverse all characters of given string.
If opening bracket , push index of it.
If closing bracket.
Pop the previous opening bracket 's index.
Check if this length formed with base of current valid substring is more than max so far.
If stack is empty . push current index as base for next valid substring ( if any ).
Driver code.
Function call.
Function call"
Solution in Python:
assistant
#Here is code to length of the longest valid substring and the other steps required.
 def findMaxLen ( string ) :
	n = len ( string )
	stk = [ ]
	stk . append ( - 1 )
	result = 0
	for i in xrange ( n ) :
		if string [ i ] == ' ( ' :
			stk . append ( i )
		else :
			if len ( stk ) != 0 :
			stk . pop ( )
			if len ( stk ) != 0 :
				result = max ( result , i - stk [ len ( stk ) - 1 ] )
			else :
				stk . append ( i )
	return result
string = " ( ( ( ) ( ) "
print findMaxLen ( string )
string = " ( ) ( ( ) ) ) ) ) "
print findMaxLen ( string )