Interview Question
Country: United States
Code for removal of character to make palindrome
#include <iostream>
#include <string>
using namespace std;
int checkForPalindrome(string str,int i,int j)
{
if(i==j)
return 1;
if(str[i]==str[j])
return 1;
if(str[i]!=str[j])
return 0;
return checkForPalindrome(str,i,j-1)&&checkForPalindrome(str,i+1,j);
}
string removeChar(string str,int i,int j)
{
if(i==j)
return str;
if(str[i]==str[j]&&i+1==j)
return str;
if(str[i]!=str[j]&&i+1==j)
{
str.erase(i,1);
return str;
}
if(str[i]==str[j])
return removeChar(str,i+1,j-1);
if(str[i]!=str[j])
{
if(checkForPalindrome(str,i,j-1))
{
str.erase(j,1);
return str;
}
else if(checkForPalindrome(str,i+1,j))
{
str.erase(i,1);
return str;
}
}
}
int main()
{
string str="fbbfe";
if(checkForPalindrome(str,0,str.length()-1)!=1)
str=removeChar(str,0,str.length()-1);
cout<<str;
}
Yes this code is only for the cases where we have to move a character. Of course for other cases we can call for checkPalindrome first and if it is already then we will not proceed. It can be included easily
Assuming we must delete a character, we can use 2 pointers: from the start and from the end of the string and check if they're equal. If not, we must remove one of them and check if the remaining string is palindrome.
We only do at most 2 checks for palindrome, so this code runs in O(n), n = string.length
bool IsPalindrome(const string&s, int from, int to) {
for (; from < to; from++, to--)
if (s[from] != s[to])
return false;
return true;
}
int DeleteCharToPalindrome(const string& s, int from, int to) {
if (from >= to) // s is palindrome. if its size is even, we can
return to; // just remove one of the middle elements if the size
// is even or the middle element if size is odd
if (s[from] == s[to])
return DeleteCharToPalindrome(s, from+1, to-1);
else if (IsPalindrome(s, from, to-1))
return to;
else if (IsPalindrome(s, from+1, to))
return from;
return -1;
}
// returns -1 if impossible, 0..len-1 otherwise representing the index to delete
int DeleteCharToPalindrome(const string& s) {
return s == "" ? -1 : DeleteCharToPalindrome(s, 0, s.size()-1);
}
Assuming there exists one character in the string the removal of which turns string into palindrome, we can do as follows:
1. Keep two pointers front=0 and back=n-1.
2. while string[front++] == string[back--] continue;
3. if string[front+1, back] is palindrome then return string without character[front]
else return string without character[back]
Edit: based on gastonbm's reply, changed last step to check for palindrome.
I think your solution it's almost complete.. but not quite.
For example if a give you string = "caaca", I should remove the last a, but your algorithm removes the c.. resulting in a non-palindrome
In step 3, instead of checking for just one character, better choice is to check for the palindrom. If the substring between front and back pointer(skipping front) is palindrom then front is the odd char or else back is the odd char
{{if(isPalin(str.substring(front, back+2)))
return charArray[front-1];
else if(isPalin(str.substring(front-1, back+1)))
return charArray[back+1];}}
#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)
{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
temp[i]=temp[strlen(temp)-i];
temp[i]='\0';
if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)
{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
temp[i]=temp[strlen(temp)-i];
temp[i]='\0';
if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)
{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
strrev(ch1);
if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}
I think your solution it's almost complete.. but not quite.
- gastonbm June 03, 2014For example if a give you string = "caaca", I should remove the last a, but your algorithm removes the c.. resulting in a non-palindrome