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 email@example.com. 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
- Smallest length string with repeated replacement of two distinct adjacent
- 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
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Balanced expression with replacement
- Count all palindrome which is square of a palindrome
- Character replacement after removing duplicates from a string
- Palindrome Partitioning | DP-17
- Nth Even length Palindrome
- Sum of first K even-length Palindrome numbers
- Next word that does not contain a palindrome and has characters from first k
- Palindrome by swapping only one character
- Palindrome Substring Queries
- Longest Possible Chunked Palindrome
- Check if a given string is Even-Odd Palindrome or not
- Print all palindrome permutations of a string
- Longest palindrome subsequence with O(n) space