Base Condition
public static int parenthesis_count ( String str , int i , int j , int isTrue , int [ ] [ ] [ ] dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str . charAt ( i ) == ' T ' ) ? 1 : 0 ; } else { return ( str . charAt ( i ) == ' F ' ) ? 1 : 0 ; } } if ( dp [ i ] [ j ] [ isTrue ] != - 1 ) return dp [ i ] [ j ] [ isTrue ] ; int temp_ans = 0 ; int leftTrue , rightTrue , leftFalse , rightFalse ; for ( int k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i ] [ k - 1 ] [ 1 ] != - 1 ) leftTrue = dp [ i ] [ k - 1 ] [ 1 ] ; else {