Store the frequency of each character of S
HashMap < Character , Integer > d = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { d . put ( s . charAt ( i ) , d . getOrDefault ( s . charAt ( i ) , 0 ) + 1 ) ; }