Initialize the dp vector
int [ ] [ ] [ ] dp = new int [ N ] [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ) ; }