Length of the longest valid substring | Python3 program to find length of the longest valid substring ; Initialize curMax to zero ; Iterate over the string starting from second index ; Driver Code ; Function call ; Function call
def findMaxLen ( s ) : NEW_LINE INDENT if ( len ( s ) <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT curMax = 0 NEW_LINE longest = [ 0 ] * ( len ( s ) ) NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( ( s [ i ] == ' ) ' and i - longest [ i - 1 ] - 1 >= 0 and s [ i - longest [ i - 1 ] - 1 ] == ' ( ' ) ) : NEW_LINE INDENT longest [ i ] = longest [ i - 1 ] + 2 NEW_LINE if ( i - longest [ i - 1 ] - 2 >= 0 ) : NEW_LINE INDENT longest [ i ] += ( longest [ i - longest [ i - 1 ] - 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT longest [ i ] += 0 NEW_LINE DEDENT curMax = max ( longest [ i ] , curMax ) NEW_LINE DEDENT DEDENT return curMax NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " ( ( ( ) ( ) " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE Str = " ( ) ( ( ) ) ) ) ) " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE DEDENT