Construct lexicographically smallest palindrome | function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; driver program
function constructPalin ( str , len ) { var i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' ' ) { str [ i ] = ' ' ; str [ j ] = ' ' ; continue ; } else if ( str [ i ] == ' ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' ' ) { str [ j ] = str [ i ] ; continue ; } document . write ( " " ) ; return " " ; } return str . join ( " " ) ; } var str = " " . split ( " " ) ; var len = str . length ; document . write ( constructPalin ( str , len ) ) ;