Given a string which has some lowercase alphabet characters and one special character dot(.). We need to replace all dots with some alphabet character in such a way that resultant string becomes a palindrome, in case of many possible replacements, we need to choose palindrome string which is lexicographically smallest. If it is not possible to convert string into palindrome after all possible replacements then output Not possible.
Input : str = “ab..e.c.a” Output : abcaeacba The smallest palindrome which can be made after replacement is "abcaeacba" We replaced first dot with "c", second dot with "a", third dot with "a" and fourth dot with "b" Input : str = “ab..e.c.b” Output : Not Possible It is not possible to convert above string into palindrome
We can solve this problem as follows, As resultant string need to be palindrome, we can check pair of non-dot characters in starting itself, if they don’t match then direct return not possible because we can place new character at position of dots only not anywhere else.
After that, we iterate over characters of string, if current character is dot, then we check its paired character (character at (n – i -1)th position), if that character is also dot, then we can replace both character by ‘a’, because ‘a’ is smallest lowercase alphabet which will guarantee smallest lexicographic string at the end, replacing both by any other character will result in lexicographically larger palindromic string. In other case, if paired character is not a dot, then to make string palindrome we must replace current character by its paired character.
So in short, If both "i", and "n- i- 1" are dot, replace them by ‘a’ If one of them is a dot character replace that by other non-dot character
Above procedure gives us lexicographically smallest palindrome string.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the smallest window in a string containing all characters of another string
- Palindrome Partitioning | DP-17
- Given a number, find the next smallest palindrome
- Minimum insertions to form a palindrome | DP-28
- Check if a given string is a rotation of a palindrome
- Online algorithm for checking palindrome in a stream
- Print all palindrome permutations of a string
- Palindrome Substring Queries
- Find longest palindrome formed by removing or shuffling chars from string
- Remove a character from a string to make it a palindrome
- Check for Palindrome after every character replacement Query
- Count All Palindrome Sub-Strings in a String | Set 1
- Minimum characters to be added at front to make string palindrome
- Queries on substring palindrome formation
- Palindrome pair in an array of words (or strings)
- Make largest palindrome by changing at most K-digits
- Character replacement after removing duplicates from a string
- Smallest length string with repeated replacement of two distinct adjacent
- Recursive function to check if a string is palindrome
- C Program to Check if a Given String is Palindrome