fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isPalindrome(const string& s, int l, int r) {
  5. while (l < r) {
  6. if (s[l++] != s[r--])
  7. return false;
  8. }
  9. return true;
  10. }
  11.  
  12.  
  13. bool check(const string& a, const string& b) {
  14. int l = 0, r = a.size() - 1;
  15. while (l < r && a[l] == b[r]) {
  16. l++;
  17. r--;
  18. }
  19. return isPalindrome(a, l, r) || isPalindrome(b, l, r);
  20. }
  21.  
  22. bool checkPalindromeFormation(string a, string b) {
  23. return check(a, b) || check(b, a);
  24. }
  25.  
  26. int main() {
  27. string a = "abdef";
  28. string b = "fecab";
  29.  
  30. if (checkPalindromeFormation(a, b))
  31. cout << "Yes, a palindrome can be formed!" << endl;
  32. else
  33. cout << "No palindrome possible from any split." << endl;
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Yes, a palindrome can be formed!