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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check for Palindrome after every character replacement Query
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Smallest length string with repeated replacement of two distinct adjacent
- Character replacement after removing duplicates from a string
- Count all palindrome which is square of a palindrome
- Balanced expression with replacement
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Given a number, find the next smallest palindrome
- Construct lexicographically smallest palindrome
- Make a lexicographically smallest palindrome with minimal changes
- Make lexicographically smallest palindrome by substituting missing characters
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Check if string remains palindrome after removing given number of characters
- Check if string is palindrome after removing all consecutive duplicates
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest string after M operations
- Lexicographical smallest number after at most K consecutive swaps
- Palindrome Partitioning | DP-17
- Minimum insertions to form a palindrome | DP-28
- Check if a given string is a rotation of a palindrome