Generate a String from given Strings P and Q based on the given conditions | Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in [ ] freq to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
function manipulateStrings ( P , Q ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < P . length ; i ++ ) { freq [ P [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < Q . length ; i ++ ) { freq [ Q [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; } var sb = " " ; var pos = Q [ 0 ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; for ( var i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { var c = String . fromCharCode ( " " . charCodeAt ( 0 ) + i ) ; sb += c ; freq [ i ] -- ; } } sb += Q ; for ( var i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { var c = String . fromCharCode ( " " . charCodeAt ( 0 ) + i ) ; sb += c ; freq [ i ] -- ; } } document . write ( sb ) ; } var P = " " ; var Q = " " ; manipulateStrings ( P , Q ) ;