Make lexicographically smallest palindrome by substituting missing characters | Function to return the lexicographically smallest palindrome that can be made from the given string after replacing the required characters ; If characters are missing at both the positions then substitute it with ' a ' ; If only str [ j ] = ' * ' then update it with the value at str [ i ] ; If only str [ i ] = ' * ' then update it with the value at str [ j ] ; If characters at both positions are not equal and != ' * ' then the string cannot be made palindrome ; Return the required palindrome ; Driver code
function makePalindrome ( str ) { var i = 0 , j = str . length - 1 ; while ( i <= j ) { if ( str [ i ] == ' ' && str [ j ] == ' ' ) { str [ i ] = ' ' ; str [ j ] = ' ' ; } else if ( str [ j ] == ' ' ) str [ j ] = str [ i ] ; else if ( str [ i ] == ' ' ) str [ i ] = str [ j ] ; else if ( str [ i ] != str [ j ] ) return " " ; i ++ ; j -- ; } return str . join ( " " ) ; } var str = " " . split ( ' ' ) ; document . write ( makePalindrome ( str ) ) ;