C++, 254 248248 246 bytes
-6 bytes thanks to Zacharý -2 bytes thanks to Toby Speight
#include<string> #define S size() #define T return using s=std::string;int p(s t){for(int i=0;i<t.S/2;++iS;++i)if(t[i]!=t[t.S-i-1])T 0;T 1;}s d(s e){if(!p(e))for(int i,w=1;w<e.S;++w)for(i=0;i<=e.S-w;++i){s t=e;t.erase(i,w);if(p(t))T e.substr(i,w);}T"";} So...
I used
Tas a macro definition because doingR""as another effect on string literal ( it's a prefix used to define raw string literals, see cppreference for more informations ) that is not there when i doT""Preprocessor definitions can't be on the same line, and have to have at least one space between the name and the content in the definition
2 functions :
p(std::string)to test if the string is a palindrome. If it is, it returns1which casts totrue, else it returns0, which casts tofalseThe algorithm loops over the whole string testing if it's a palindrome when erasing each time 1 element, then test erasing 2 elements ( loops over that to the maximum size of the string ), from the first index to
the last index - number of erased char. If it finds erasing some part is a palindrome, then, it returns. For example, when passing the string"aabcdbaa"as parameter, bothcanddare valid answer, but this code will returncbecause erasing it and testing if it's a palindrome comes before testing if erasingdand testing if it's palindromeHere is the code to test :
std::initializer_list<std::pair<std::string, std::string>> test{ {"800233008","2"}, { "racecarFOOL","FOOL" }, { "abcdedcba","" }, { "ngryL Myrgn","L " }, { "123456789","12345678" }, { "aabcdbaa","c" }, { "[[]]","[[" }, { "a","" }, { "aabaab","b" } }; for (const auto& a : test) { if (a.second != d(a.first)) { std::cout << "Error on : " << a.first << " - Answer : " << a.second << " - Current : " << d(a.first) << '\n'; } }