VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-character-string-make-palindrome/

⇱ Remove a character from a string to make it a palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove a character from a string to make it a palindrome

Last Updated : 6 Jul, 2022

Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this. 

Examples:  

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. 

Implementation:


Output
Not Possible 

Time complexity : O(N)
Space Complexity: O(1)

Comment
Article Tags:
Article Tags: