Reorder characters of a string to valid English representations of digits | Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Given string , s ; Function Call
function construct_digits ( s ) { let k = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let l = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] ; let ans = [ ] ; let d = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! d . has ( s [ i ] ) ) d . set ( s [ i ] , 0 ) ; d . set ( s [ i ] , d . get ( s [ i ] ) + 1 ) ; } for ( let i = 0 ; i < k . length ; i ++ ) { let x = 0 ; if ( d . has ( k [ i ] ) ) x = d . get ( k [ i ] ) ; for ( let j = 0 ; j < l [ i ] . length ; j ++ ) { if ( d . has ( l [ i ] [ j ] ) ) d . set ( l [ i ] [ j ] , d . get ( l [ i ] [ j ] ) - x ) ; } for ( let j = 0 ; j < x ; j ++ ) ans . push ( c [ i ] ) ; } ans . sort ( ) ; return ans . join ( " " ) ; } let s = " " ; document . write ( construct_digits ( s ) ) ;