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:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string::iterator low, string::iterator high)
{
while (low < high)
{
if (*low != *high)
return false ;
low++;
high--;
}
return true ;
}
int possiblePalinByRemovingOneChar(string str)
{
int low = 0, high = str.length() - 1;
while (low < high)
{
if (str[low] == str[high])
{
low++;
high--;
}
else
{
if (isPalindrome(str.begin() + low + 1, str.begin() + high))
return low;
if (isPalindrome(str.begin() + low, str.begin() + high - 1))
return high;
return -1;
}
}
return -2;
}
int main()
{
string str = "abecbea" ;
int idx = possiblePalinByRemovingOneChar(str);
if (idx == -1)
cout << "Not Possible \n" ;
else if (idx == -2)
cout << "Possible without removing any character" ;
else
cout << "Possible by removing character"
<< " at index " << idx << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPalindrome(String str,
int low, int high)
{
while (low < high)
{
if (str.charAt(low) != str.charAt(high))
return false ;
low++;
high--;
}
return true ;
}
static int possiblePalinByRemovingOneChar(String str)
{
int low = 0 , high = str.length() - 1 ;
while (low < high)
{
if (str.charAt(low) == str.charAt(high))
{
low++;
high--;
}
else
{
if (isPalindrome(str, low + 1 , high))
return low;
if (isPalindrome(str, low, high - 1 ))
return high;
return - 1 ;
}
}
return - 2 ;
}
public static void main(String[] args)
{
String str = "abecbea" ;
int idx = possiblePalinByRemovingOneChar(str);
if (idx == - 1 )
System.out.println( "Not Possible" );
else if (idx == - 2 )
System.out.println( "Possible without " +
"removing any character" );
else
System.out.println( "Possible by removing" +
" character at index " + idx);
}
}
|
Python3
def isPalindrome(string: str , low: int , high: int ) - > bool :
while low < high:
if string[low] ! = string[high]:
return False
low + = 1
high - = 1
return True
def possiblepalinByRemovingOneChar(string: str ) - > int :
low = 0
high = len (string) - 1
while low < high:
if string[low] = = string[high]:
low + = 1
high - = 1
else :
if isPalindrome(string, low + 1 , high):
return low
if isPalindrome(string, low, high - 1 ):
return high
return - 1
return - 2
if __name__ = = "__main__" :
string = "abecbea"
idx = possiblepalinByRemovingOneChar(string)
if idx = = - 1 :
print ( "Not possible" )
else if idx = = - 2 :
print ( "Possible without removing any character" )
else :
print ( "Possible by removing character at index" , idx)
|
C#
using System;
class GFG
{
static bool isPalindrome( string str, int low, int high)
{
while (low < high)
{
if (str[low] != str[high])
return false ;
low++;
high--;
}
return true ;
}
static int possiblePalinByRemovingOneChar( string str)
{
int low = 0, high = str.Length - 1;
while (low < high)
{
if (str[low] == str[high])
{
low++;
high--;
}
else
{
if (isPalindrome(str, low + 1, high))
return low;
if (isPalindrome(str, low, high - 1))
return high;
return -1;
}
}
return -2;
}
public static void Main(String[] args)
{
string str = "abecbea" ;
int idx = possiblePalinByRemovingOneChar(str);
if (idx == -1)
Console.Write( "Not Possible" );
else if (idx == -2)
Console.Write( "Possible without " +
"removing any character" );
else
Console.Write( "Possible by removing" +
" character at index " + idx);
}
}
|
Javascript
<script>
function isPalindrome(str, low, high)
{
while (low < high)
{
if (str.charAt(low) != str.charAt(high))
return false ;
low++;
high--;
}
return true ;
}
function possiblePalinByRemovingOneChar(str)
{
var low = 0, high = str.length - 1;
while (low < high)
{
if (str.charAt(low) == str.charAt(high))
{
low++;
high--;
}
else
{
if (isPalindrome(str, low + 1, high))
return low;
if (isPalindrome(str, low, high - 1))
return high;
return -1;
}
}
return -2;
}
var str = "abecbea" ;
var idx = possiblePalinByRemovingOneChar(str);
if (idx == -1)
document.write( "Not Possible" );
else if (idx == -2)
document.write( "Possible without " +
"removing any character" );
else
document.write( "Possible by removing" +
" character at index " + idx);
</script>
|
Time complexity : O(N)
Space Complexity: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Jul, 2022
Like Article
Save Article