Smallest Palindrome after replacement | C ++ 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 palindrom 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 to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossiblePalindrome ( string 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 ; } string smallestPalindrome ( string str ) { if ( ! isPossiblePalindrome ( str ) ) return " 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 ' ; } } return str ; } int main ( ) { string str = " ab . . e . c . a " ; cout << smallestPalindrome ( str ) << endl ; return 0 ; }