Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this.
Input : str = “abcba” Output : Yes we can remove character ‘c’ to make string palindrome Input : str = “abcbea” Output : Yes we can remove character ‘e’ to make string palindrome Input : str = “abecbea” It is not possible to make this string palindrome just by removing one character
We can solve this problem by finding the position of mismatch. We start looping in the string by keeping two pointers at both the ends which traverse towards mid position after each iteration, this iteration will stop when we find a mismatch, as it is allowed to remove just one character we have two choices here,
At mismatch, either remove character pointed by left pointer or remove character pointed by right pointer.
We will check both the cases, remember as we have traversed equal number of steps from both sides, this mid string should also be a palindrome after removing one character, so we check two substrings, one by removing left character and one by removing right character and if one of them is palindrome then we can make complete string palindrome by removing corresponding character, and if both substrings are not palindrome then it is not possible to make complete string a palindrome under given constraint.
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.
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
- Convert the string into palindrome string by changing only one character.
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Number of Counterclockwise shifts to make a string palindrome
- Remove the first and last character of each word in a string
- Remove all occurrences of a character in a string
- Minimum number of Appends needed to make a string palindrome
- Minimum characters to be added at front to make string palindrome
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Palindrome by swapping only one character
- Check for Palindrome after every character replacement Query
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
Improved By : sanjeev2552