Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant .
while ( k < n ) { while ( l < MAX && uCount [ l ] === 0 ) l ++ ; if ( l < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] === 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + j ) ; lCount [ j ] -- ; } } return s . join ( " " ) ; }