declare the map for store each char with occurrence
HashMap < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) mp . put ( str . charAt ( i ) , mp . get ( str . charAt ( i ) ) == null ? 1 : mp . get ( str . charAt ( i ) ) + 1 ) ;