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 A[0, i] and B[i, N – 1] or A[i, N – 1] and B[0, i] respectively, form 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)
Alternate Python Approach(Two pointers +Slicing):
This approach follows the following path:
- Define the function check
- Take two pointers i,j at 0, n-1 position respectively
- If the first character of a and second character of b does not match we break (since we are searching for the palindrome)
- If matches we simply increment the pointer
- We would concatenate the form a[i:j+1] of the first string and b[i:j+1] of the second string and check for the condition of palindrome
- If yes we return True
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
- Check whether given string can be generated after concatenating given strings
- Longest palindromic string possible by concatenating strings from a given array
- 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
- Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if K palindromic strings can be formed from a given string
- Sum of all possible strings obtained by removal of non-empty substrings
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Reverse the substrings of the given String according to the given Array of indices
- Maximize cost obtained by removal of substrings "pr" or "rp" from a given String
- Check if given string can be split into four distinct strings
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Check if a string can be obtained by rotating another string 2 places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.