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”.
Input: A = “x”, B = “y”
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”
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:
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.
- 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 firstname.lastname@example.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.