Final string after performing given operations | Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Program to test above function
function printFinalString ( s ) { var i , n ; n = s . length ; var x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) x ++ ; else y ++ ; } var finalString = " " ; if ( x > y ) for ( i = 0 ; i < x - y ; i ++ ) finalString += " " ; else for ( i = 0 ; i < y - x ; i ++ ) finalString += " " ; return finalString ; } var s = " " ; document . write ( printFinalString ( s ) ) ;