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 ++ ] = ( char ) ( ' A ' + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] == 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = ( char ) ( ' a ' + j ) ; lCount [ j ] -- ; } } return ( new String ( s ) ) ; }