Put all obtained palindromes in a hash map to find only distinct palindromess
if ( ! m . ContainsKey ( s . Substring ( 0 , 1 ) ) ) m . Add ( s . Substring ( 0 , 1 ) , 1 ) ; else m [ s . Substring ( 0 , 1 ) ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= 1 ; j ++ ) for ( int rp = R [ j , i ] ; rp > 0 ; rp -- ) { if ( ! m . ContainsKey ( s . Substring ( i - rp - 1 , 2 * rp + j ) ) ) m . Add ( s . Substring ( i - rp - 1 , 2 * rp + j ) , 1 ) ; else m [ s . Substring ( i - rp - 1 , 2 * rp + j ) ] ++ ; } if ( ! m . ContainsKey ( s . Substring ( i , 1 ) ) ) m . Add ( s . Substring ( i , 1 ) , 1 ) ; else m [ s . Substring ( i , 1 ) ] ++ ; }