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
< ? php function isPossiblePalindrome ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ 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 ) ) return " Not ▁ Possible " ; $ n = strlen ( $ str ) ; for ( $ 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 ; } $ str = " ab . . e . c . a " ; echo smallestPalindrome ( $ str ) ; ? >