Traverse the pattern and print every characters same number of times as it appears in str . This loop takes O ( m + n ) time where m is length of pattern and n is length of str .
let index = 0 ; for ( let i = 0 ; i < pat . length ; i ++ ) { for ( let j = 0 ; j < count [ pat [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; j ++ ) { str [ index ++ ] = pat [ i ] ; } } }