Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
function countFrequency ( S , N ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { freq [ S [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] === 0 ) continue ; var lg = parseInt ( Math . log2 ( freq [ i ] ) ) ; var a = Math . pow ( 2 , lg ) ; if ( a === freq [ i ] ) { while ( freq [ i ] -- ) document . write ( String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ) ; } } } var S = " " ; var N = S . length ; countFrequency ( S , N ) ;