Smallest Palindrome after replacement | Utility method to check str is possible palindrome after ignoring ; If both left and right character are not dot and they are not equal also , then it is not possible to make this string a palindrome ; Returns lexicographically smallest palindrome string , if possible ; loop through character of string ; if one of character is dot , replace dot with other character ; if both character are dot , then replace them with smallest character ' a ' ; return the result ; Driver code
function isPossiblePalindrome ( str ) { let n = str . length ; for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { if ( str [ i ] != ' ' && str [ n - i - 1 ] != ' ' && str [ i ] != str [ n - i - 1 ] ) return false ; } return true ; } function smallestPalindrome ( str ) { if ( ! isPossiblePalindrome ( str ) ) document . write ( " " ) ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { if ( str [ n - i - 1 ] != ' ' ) str [ i ] = str [ n - i - 1 ] ; else str [ i ] = str [ n - i - 1 ] = ' ' ; } } for ( let i = 0 ; i < n ; i ++ ) document . write ( str [ i ] + " " ) ; } let str = " " ; let s = str . split ( " " ) ; smallestPalindrome ( s ) ;