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); } } |
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.
Recommended Posts:
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Check if a string can be split into even length palindromic substrings
- Longest palindromic string possible by concatenating strings from a given array
- Check whether given string can be generated after concatenating given strings
- Check if a string can be split into two strings with same number of K-frequent characters
- Lexicographically smallest string obtained after concatenating array
- Split string into three palindromic substrings with earliest possible cuts
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if a string can be split into substrings starting with N followed by N characters
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Maximize cost obtained by removal of substrings "pr" or "rp" from a given String
- Reverse the substrings of the given String according to the given Array of indices
- Check if K palindromic strings can be formed from a given string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
- Check if given string can be split into four distinct strings
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Pairs of strings which on concatenating contains each character of "string"
- Maximize length of the String by concatenating characters from an Array of Strings
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.

