Length of the longest valid substring | Java 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
import java . util . * ; class GFG { static int findMaxLen ( String s ) { if ( s . length ( ) <= 1 ) return 0 ; int curMax = 0 ; int [ ] longest = new int [ s . length ( ) ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ) ' && i - longest [ i - 1 ] - 1 >= 0 && s . charAt ( i - longest [ i - 1 ] - 1 ) == ' ( ' ) { longest [ i ] = longest [ i - 1 ] + 2 + ( ( i - longest [ i - 1 ] - 2 >= 0 ) ? longest [ i - longest [ i - 1 ] - 2 ] : 0 ) ; curMax = Math . max ( longest [ i ] , curMax ) ; } } return curMax ; } public static void main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); str = " ( ) ( ( ) ) ) ) ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); } }