# Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings

Given two strings A and B of length N, the task is to check if any of the two strings formed by splitting both the strings at any index i (0 ≤ i ≤ N – 1) and concatenating concatenating A[0, i] and B[i, N – 1] or A[i, N – 1] and B[0, i] respectively, forms a palindromic string or not. If found to be true, print “Yes”. Otherwise, print “No”.

Examples:

Input: A = “x”, B = “y”
Output: Yes
Explanation:
Let the string be spitted at index 0 then,
Split A from index 0: “”+”x” A[0, 0] = “” and A[1, 1] = “x”.
Split B from index 0: “”+”y” B[0, 0] = “” and B[1, 1] = “y”.
The concatenation of A[0, 0] and B[1, 1] is = “y” which is a palindromic string.

Input: A = “xbdef”, B = “xecab”
Output: No

Approach: The idea is to use the Two Pointer Technique and traverse the string over the range [0, N – 1] and check if the concatenation of substrings A[0, i – 1] and B[i, N – 1] or the concatenation of substrings A[i, N – 1] and B[0, i – 1] is a palindromic or not. If any of the concatenation is found to be palindromic then print “Yes” else print “No”.

Below is the implementation of the above approach:

## Java

 `// Java program for the above approach ` ` `  `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to check if a string ` `    ``// is palindrome or not ` `    ``static` `boolean` `isPalindrome(String str) ` `    ``{ ` ` `  `        ``// Start and end of the string ` `        ``int` `i = ``0``, j = str.length() - ``1``; ` ` `  `        ``// Iterate the string until i > j ` `        ``while` `(i < j) { ` ` `  `            ``// If there is a mismatch ` `            ``if` `(str.charAt(i) ` `                ``!= str.charAt(j)) ` `                ``return` `false``; ` ` `  `            ``// Increment first pointer and ` `            ``// decrement the other ` `            ``i++; ` `            ``j--; ` `        ``} ` ` `  `        ``// Given string is a palindrome ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Function two check if the strings can ` `    ``// be combined to form a palindrome ` `    ``static` `void` `formPalindrome( ` `        ``String a, String b, ``int` `n) ` `    ``{ ` `        ``// Initialize array of characters ` `        ``char` `aa[] = ``new` `char``[n + ``2``]; ` `        ``char` `bb[] = ``new` `char``[n + ``2``]; ` ` `  `        ``Arrays.fill(aa, ``' '``); ` `        ``Arrays.fill(bb, ``' '``); ` ` `  `        ``// Stores character of string ` `        ``// in the character array ` `        ``for` `(``int` `i = ``1``; i <= n; i++) { ` `            ``aa[i] = a.charAt(i - ``1``); ` `            ``bb[i] = b.charAt(i - ``1``); ` `        ``} ` ` `  `        ``boolean` `ok = ``false``; ` ` `  `        ``for` `(``int` `i = ``0``; i <= n + ``1``; i++) { ` ` `  `            ``// Find left and right parts ` `            ``// of strings a and b ` `            ``StringBuilder la ` `                ``= ``new` `StringBuilder(); ` `            ``StringBuilder ra ` `                ``= ``new` `StringBuilder(); ` `            ``StringBuilder lb ` `                ``= ``new` `StringBuilder(); ` `            ``StringBuilder rb ` `                ``= ``new` `StringBuilder(); ` ` `  `            ``for` `(``int` `j = ``1``; ` `                 ``j <= i - ``1``; j++) { ` ` `  `                ``// Substring a[j...i-1] ` `                ``la.append((aa[j] == ``' '``) ` `                              ``? ``""` `                              ``: aa[j]); ` ` `  `                ``// Substring b[j...i-1] ` `                ``lb.append((bb[j] == ``' '``) ` `                              ``? ``""` `                              ``: bb[j]); ` `            ``} ` ` `  `            ``for` `(``int` `j = i; ` `                 ``j <= n + ``1``; j++) { ` ` `  `                ``// Substring a[i...n] ` `                ``ra.append((aa[j] == ``' '``) ` `                              ``? ``""` `                              ``: aa[j]); ` ` `  `                ``// Substring b[i...n] ` `                ``rb.append((bb[j] == ``' '``) ` `                              ``? ``""` `                              ``: bb[j]); ` `            ``} ` ` `  `            ``// Check is left part of a + ` `            ``// right part of b or vice ` `            ``// versa is a palindrome ` `            ``if` `(isPalindrome(la.toString() ` `                             ``+ rb.toString()) ` `                ``|| isPalindrome(lb.toString() ` `                                ``+ ra.toString())) { ` `                ``ok = ``true``; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// Print the result ` `        ``if` `(ok) ` `            ``System.out.println(``"Yes"``); ` ` `  `        ``// Otherwise ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String A = ``"bdea"``; ` `        ``String B = ``"abbb"``; ` ` `  `        ``int` `N = ``4``; ` ` `  `        ``formPalindrome(A, B, N); ` `    ``} ` `} `

Output:

```Yes
```

Time Complexity: O(N2) where N is the lengths of the given strings.
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.