Store number of occurrences of each character and position of each chatacter in string
for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) pref [ j ] [ i ] += pref [ j ] [ i - 1 ] ; int index = s [ i ] - ' a ' ; pref [ index ] [ i ] ++ ; pos [ index ] . push_back ( i ) ; } int ans = 0 ;