For storing the value of all states .
int [ , ] memoize = new int [ n + 1 , 3 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { memoize [ i , j ] = - 1 ; } } int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) {