Skip to main content
1 of 3
HatsuPointerKun
  • 2.2k
  • 1
  • 9
  • 13

C++, 254 bytes

#include<string> #define S size() #define T return using s=std::string;int p(s t){for(int i=0;i<t.S/2;++i)if(t[i]!=t[t.S-i-1])T 0;T 1;}s d(s e){if(!p(e)){int i,w=1;while(w<e.S){for(i=0;i<=e.S-w;++i){s t=e;t.erase(i,w);if(p(t))T e.substr(i,w);}++w;}}T"";} 

So...

  • I used T as a macro definition because doing R"" as another effect that is not there when i do T""

  • 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 returns 1 which casts to true, else it returns 0, which casts to false

  • The 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, both c and d are valid answer, but this code will return c because erasing it and testing if it's a palindrome comes before testing if erasing d and testing if it's palindrome

  • Here 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'; } } 
HatsuPointerKun
  • 2.2k
  • 1
  • 9
  • 13