Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2 | Function to check if N is power of 2 or not ; Base Case ; Return true if N is power of 2 ; Function to print array of strings in ascending order ; Sort strings in ascending order ; Print the array ; Function to sort the strings after modifying each string according to the given conditions ; Store the frequency of each characters of the string ; Stores the required array of strings ; Traverse the array of strings ; Temporary string ; Stores frequency of each alphabet of the string ; Update frequency of S [ i ] [ j ] ; Traverse the map freq ; Check if the frequency of i . first is a power of 2 ; Update string st ; Clear the map ; Null string ; Sort the string in descending order ; Update res ; Print the array of strings ; Driver Code
function isPowerOfTwo ( n ) { if ( n == 0 ) return false ; return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } function printArray ( res ) { res . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } function sortedStrings ( s , N ) { let freq = new Map ( ) ; let res = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let st = " " ; for ( let j = 0 ; j < s [ i ] . length ; j ++ ) { if ( freq . has ( s [ i ] [ j ] ) ) { freq . set ( s [ i ] [ j ] , freq . get ( s [ i ] [ j ] ) + 1 ) } else { freq . set ( s [ i ] [ j ] , 1 ) } } for ( let ch of freq . keys ( ) ) { if ( isPowerOfTwo ( freq . get ( ch ) ) ) { for ( let j = 0 ; j < freq . get ( ch ) ; j ++ ) { st += ch ; } } } freq . clear ( ) ; if ( st . length == 0 ) continue ; let myCharArr = st . split ( " " ) ; myCharArr . sort ( ) ; let ns = " " ; for ( let j = myCharArr . length - 1 ; j >= 0 ; -- j ) ns += myCharArr [ j ] ; res . push ( ns ) ; } printArray ( res ) ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; sortedStrings ( arr , N ) ;