human
Length of the longest valid substring | Function to return the length of the longest valid substring.
Variables for left and right counter . maxlength to store the maximum length found so far.
Iterating the string from left to right.
If " ( " is encountered , then left counter is incremented else right counter is incremented.
Whenever left is equal to right , it signifies that the subsequence is valid and.
Reseting the counters when the subsequence becomes invalid.
Iterating the string from right to left.
If " ( " is encountered , then left counter is incremented else right counter is incremented.
Whenever left is equal to right , it signifies that the subsequence is valid and.
Reseting the counters when the subsequence becomes invalid.
Function call"
Solution in Python:

=====
Write me a code to length of the longest valid substring and the other steps required?
assistant
#Here is code to length of the longest valid substring and the other steps required.
 def solve ( s , n ) :
	left = 0
	right = 0
	maxlength = 0
	for i in range ( n ) :
		if ( s [ i ] == ' ( ' ) :
			left += 1
		else :
			right += 1
		if ( left == right ) :
			maxlength = max ( maxlength , 2 * right )
		elif ( right > left ) :
			left = right = 0
	left = right = 0
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( s [ i ] == ' ( ' ) :
			left += 1
		else :
			right += 1
		if ( left == right ) :
			maxlength = max ( maxlength , 2 * left )
		elif ( left > right ) :
			left = right = 0
	return maxlength
print ( solve ( " ( ( ( ) ( ) ( ) ( ) ( ( ( ( ) ) " , 16 ) )