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.
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.
- Convert the string into palindrome string by changing only one character.
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Minimum characters to be added at front to make string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Number of Counterclockwise shifts to make a string palindrome
- Minimum number of Appends needed to make a string palindrome
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Remove all occurrences of a character in a string
- Remove the first and last character of each word in a string
- Remove the first and last occurrence of a given Character from a String
Improved By : sanjeev2552