Store the frequency of each character of S
Dictionary < char , int > d = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! d . ContainsKey ( s [ i ] ) ) d [ s [ i ] ] = 0 ; d [ s [ i ] ] += 1 ; }