# Write a program in C++ to split two strings to make it a palindrome

A string is said to be a palindromic string if it remains the same after reversing it.

In this particular problem, we've given two strings 'a' and 'b' of the same length. If they are split with some indexes, then the task is to check whether the sum of the strings makes a palindrome or not.

Let's say we have two string 'a' and 'b' of length '4' and after splitting both the string at the index '3' such that,

aaa | b  and bbb | a

aaa (prefix of first string) + a(suffix of second string) should be Palindrome

OR

b(suffix of first string) + bbb(prefix of second string) should be Palindrome

For Example

Input-1:

a = “abcdef”
b = “fedcba”

Output:

True

Explanation: After splitting String 'a' and String 'b' at the index '2', they will become,

abc | def    and    fed | cba

Such that abc(prefix of first string) + cba(suffix of second string) makes a palindromic string. Thus we will return “True”.

Input-2:

a =  “eatable”b =  “tableau”

Output:

False

Explanation: There are no possible ways to make these two strings palindrome.

## Approach to Solve this Problem

To solve this particular problem, we will use the two-pointer approach. In this approach, first, we will initialize two pointers, low and high, such that low points to '0' and high points to the last character of the string.

Since the length of both the strings is equal, we will check if any of them has less than 2 characters. If yes, we will return True. Otherwise, we will check recursively by iterating over the whole string with the help of pointers. If both the strings are equal, then return True, otherwise False.

• Take two strings, 'a' and 'b' respectively.
• A Boolean function checkPalindromic(string a, string b) takes two strings as input parameters and returns True or False accordingly.
• Initialize two pointers, low and high, with low = 0 and high = length of string 'b'.
• Iterate over the string and check if the characters of both strings are equal or not.
• A Boolean function split(string a, string b) takes two strings and returns True if they are palindrome, otherwise False.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string a, int low, int high) {
while (low < high) {
if (a[low] != a[high])
return false;
low++;
high--;
}
return true;
}
bool Split(string a, string b) {
int low = 0;
int high = b.size() - 1;
while (low < high and a[low] == b[high]) {
low++;
high--;
}
return isPalindrome(a, low, high) || isPalindrome(b, low, high);
}
bool checkPalindromic(string a, string b) {
if (a.size() < 2)
return true;
return Split(a, b) || Split(b, a);
}
int main() {
string a = "abcpqr";
string b = "mnocba";
if (checkPalindromic(a, b)) {
cout << "True" << endl;
} else {
cout << "False" << endl;
}
return 0;
}

Running the above code will generate the output as,

## Output

True

Explanation: If we split the given strings 'abcpqr' and 'mnocba' at the index '2' such that,

a(prefix) = abc   and   b(suffix) = cba

a(suffix) = pqr  and  b(prefix) = mno

We can observe that a(prefix) + b(suffix) makes a palindrome, therefore the output is True.