When size of String is greater than k to store the frequencies of the characters
HashMap < Character , Integer > freq = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( freq . containsKey ( s . charAt ( i ) ) ) { freq . put ( s . charAt ( i ) , freq . get ( s . charAt ( i ) ) + 1 ) ; } else { freq . put ( s . charAt ( i ) , 1 ) ; }