Count all distinct characters .
int dist_count = 0 ; unordered_map < int , int > hash_map ; for ( int i = 0 ; i < n ; i ++ ) { hash_map [ str [ i ] ] ++ ; } dist_count = hash_map . size ( ) ; int size = INT_MAX ; string res ;