Add Q to the resulting string
sb . append ( Q ) ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb . append ( c ) ; freq [ i ] -- ; } } System . out . println ( sb ) ; }