This code determines which index for a given string if that character is removed will produce a palindrome string.
For eg s = "baa" After removing b the string "aa" is palindrome.
Here this is what I have written until now. Due to time complexity problems, it exceeds 2ms. How this can be made faster?
#include <iostream>
#include <string>
using namespace std;
bool checkPalindrome(string s){
bool found = true;
int len = s.length();
for(int i=0;i<len;i++){
if(s[i] != s[len-i-1])
{
found = false;
break;
}
}
return found;
} // end of function
int main() {
int test;
cin>>test; // get the number of test case
for(int i=0;i<test;i++){
string s;
cin>>s; // get the string as input
if(checkPalindrome(s)){
cout<<-1<<endl; //if already string is palindrome
}
else{
int len = s.length();
for(int i=0;i<len;i++){
string aa = s.substr(0,i) + s.substr(i+1,len);
if(checkPalindrome(aa)){
cout<<i<<endl;
break;
}
}
}
}
return 0;
}