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 ] . Add ( i ) ; } int ans = 0 ;