Smallest Palindrome after replacement | Java program to get lexicographically smallest palindrome string ; 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
class GFG { static boolean isPossiblePalindrome ( char str [ ] ) { int n = str . length ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != ' . ' && str [ n - i - 1 ] != ' . ' && str [ i ] != str [ n - i - 1 ] ) return false ; } return true ; } static void smallestPalindrome ( char str [ ] ) { if ( ! isPossiblePalindrome ( str ) ) System . out . println ( " Not ▁ Possible " ) ; int n = str . length ; for ( int 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 ] = ' a ' ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str [ i ] + " " ) ; } public static void main ( String [ ] args ) { String str = " ab . . e . c . a " ; char [ ] s = str . toCharArray ( ) ; smallestPalindrome ( s ) ; } }