Given a string str some of whose characters are missing and are represented by a ‘*’. The task is to substitute the missing characters so as to make the lexicographically smallest palindrome. If it ot possible to make the string palindrome then print -1.
Input: str = “ab*a”
We can’t make it palindrome so output is -1.
- Place the ‘i’ marker at the starting of string and ‘j’ marker at the end of the string.
- If characters at both the positions are missing then substitute both the characters with ‘a’ so as to make it lexicographically smallest palindrome.
- If character at only ith or jth position is missing then replace it with jth or ith character respectively.
- If character at ith and jth positions are not equal then the string cannot be made into a palindrome and print -1.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Make a lexicographically smallest palindrome with minimal changes
- Construct lexicographically smallest palindrome
- Missing characters to make a string Pangram
- Swap all occurrences of two characters to get lexicographically smallest string
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Print the arranged positions of characters to make palindrome
- Minimum characters to be added at front to make string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Replace minimal number of characters to make all characters pair wise distinct
- Min flips of continuous characters to make all characters same in a string
- Minimum characters to be replaced to make frequency of all characters same
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest rotated sequence | Set 2