Given two strings str1 and str2, the task is to check if it is possible to form a Palindromic String by concatenation of two substrings of str1 and str2.
Input: str1 = “abcd”, str2 = “acba”
There are five possible cases where concatenation of two substrings from str1 and str2 gives palindromic string:
“ab” + “a” = “aba”
“ab” + “ba” = “abba”
“bc” + “cb” = “bccb”
“bc” + “b” = “bcb”
“cd” + “c” = “cdc”
Input: str1 = “pqrs”, str2 = “abcd”
There is no possible concatenation of sub-strings from given strings which gives palindromic string.
The simplest approach to solve the problem is to generate every possible substring of str1 and str2 and combine them to generate all possible concatenations. For each concatenation, check if it is palindromic or not. If found to be true, print “Yes”. Otherwise, print “No”.
Time Complexity: O(N2* M2 * (N+M)), where N and M are the lengths of str1 and str2 respectively.
Auxiliary Space: O(1)
To optimize the above approach, the following observation needs to be made:
If the given strings possess at least one common character, then they will always form a palindromic string on concatenation of the common character from both the strings.
str1 = “abc”, str2 = “fad”
Since ‘a’ is common in both strings, a palindromic string “aa” can be obtained.
Follow the steps below to solve the problem:
- Initialize a boolean array to mark the presence of each alphabet in the two strings.
- Traverse str1 and mark the index (str1[i] – ‘a’) as true.
- Now, traverse str2 and check if any index (str2[i] – ‘a’) is already marked as true, print “Yes”.
- After complete traversal of str2, if no common character is found, print “No”.
Below is the implementation of the above approach:
Time Complexity: O(max(N, M)) where N and M are the lengths of str1 and str2 respectively.
Auxiliary Space: O(1)
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 K palindromic strings can be formed from a given string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Print all possible palindromic string formed using any pair of given strings
- Check whether given string can be generated after concatenating given strings
- Longest palindromic String formed using concatenation of given strings in any order
- Check if a string can be split into even length palindromic substrings
- Longest palindrome formed by concatenating and reordering strings of equal length
- Check if given string can be formed by two other strings or their permutations
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Pairs of strings which on concatenating contains each character of "string"
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Generate a String of having N*N distinct non-palindromic Substrings
- Lexicographically all Shortest Palindromic Substrings from a given string
- Maximize length of the String by concatenating characters from an Array of Strings
- Permutation of given string that maximizes count of Palindromic substrings
- Rearrange the string to maximize the number of palindromic substrings
- Get K-th letter of the decoded string formed by repeating substrings
- Check if all the palindromic sub-strings are of odd length
- Find all palindromic sub-strings of a given string | Set 2
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.