# Print all possible palindromic string formed using any pair of given strings

Given an array of strings arr[] containing N words, the task is to print all possible palindromic string by combining any two strings from the given array.
Examples:

Input: arr[] = [“geekf”, “geeks”, “or”, “keeg”, “abc”, “ba”]
Output: [“geekfkeeg”, “geekskeeg”, “abcba”]
Explanation:
Below pairs forms the palindromic string on combining:
1. “geekf” + “keeg” = “geekfkeeg”
2. “geeks” + “keeg” = “geekskeeg”
3. “abc” + “ba” = “abcba”

Input: arr[] = [“dcb”, “yz”, “xy”, “efg”, “yx”]
Output: [“xyyx”, “yxxy”]
Explanation:
1. “xy” + “yz” = “xyyz”
2. “yx” + “xy” = “yxxy”

Naive Approach: The naive approach is to iterate all possible pairs in the given array of strings and check whether concatenation of the string forms palindromic or not. If yes then print that pair and check for the next pairs.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized using Hashing. Below are the steps:

1. Store all the words in a Map with words as keys and indices as values.
2. For each word(say str) in the arr[] break the string into string s1 and s2 such that s1 + s2 = str.
3. After the above step there arise two cases:
• Case 1: If s1 is a palindromic string and the reverse of s2 is present in the hash-map then we get the required pair (reverse of s2  + current word).
• Case 2: If s2 is a palindromic string and if reverse s1 is present in the hash-map then we get a pair (current word  +  reverse of s1).
4. Repeat the above steps for all the strings in the array.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check whether the string ` `// word is palindrome or not ` `bool` `ispalin(string word) ` `{ ` ` `  `    ``if` `(word.length() == 1 ` `        ``|| word.empty()) { ` `        ``return` `true``; ` `    ``} ` ` `  `    ``int` `l = 0; ` `    ``int` `r = word.length() - 1; ` ` `  `    ``// Iterate word ` `    ``while` `(l <= r) { ` ` `  `        ``if` `(word[l] != word[r]) { ` `            ``return` `false``; ` `        ``} ` `        ``l++; ` `        ``r--; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Function to find the palindromicPairs ` `vector ` `palindromicPairs(vector& words) ` `{ ` ` `  `    ``vector output; ` `    ``if` `(words.size() == 0 ` `        ``|| words.size() == 1) { ` `        ``return` `output; ` `    ``} ` ` `  `    ``// Insert all the strings with ` `    ``// their indices in the hash map ` `    ``unordered_map mp; ` ` `  `    ``for` `(``int` `i = 0; i < words.size(); i++) { ` `        ``mp[words[i]] = i; ` `    ``} ` ` `  `    ``// Iterate over all the words ` `    ``for` `(``int` `i = 0; i < words.size(); i++) { ` ` `  `        ``// If the word is empty then ` `        ``// we simply pair it will all the ` `        ``// words which are palindrome ` `        ``// present in the array ` `        ``// except the word itself ` `        ``if` `(words[i].empty()) { ` ` `  `            ``for` `(``auto` `x : mp) { ` ` `  `                ``if` `(x.second == i) { ` `                    ``continue``; ` `                ``} ` ` `  `                ``if` `(ispalin(x.first)) { ` `                    ``output.push_back(x.first); ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// Create all possible substrings ` `        ``// s1 and s2 ` `        ``for` `(``int` `j = 0; ` `             ``j < words[i].length(); j++) { ` ` `  `            ``string s1 = words[i].substr(0, j + 1); ` `            ``string s2 = words[i].substr(j + 1); ` ` `  `            ``// Case 1 ` `            ``// If s1 is palindrome and ` `            ``// reverse of s2 is ` `            ``// present in hashmap at ` `            ``// index other than i ` `            ``if` `(ispalin(s1)) { ` ` `  `                ``reverse(s2.begin(), ` `                        ``s2.end()); ` ` `  `                ``string temp = s2; ` ` `  `                ``if` `(mp.count(s2) == 1 ` `                    ``&& mp[s2] != i) { ` `                    ``string ans = s2 + words[i]; ` `                    ``output.push_back(ans); ` `                ``} ` ` `  `                ``s2 = temp; ` `            ``} ` ` `  `            ``// Case 2 ` `            ``// If s2 is palindrome and ` `            ``// reverse of s1 is ` `            ``// present in hashmap ` `            ``// at index other than i ` `            ``if` `(ispalin(s2)) { ` ` `  `                ``string temp = s1; ` `                ``reverse(s1.begin(), ` `                        ``s1.end()); ` ` `  `                ``if` `(mp.count(s1) == 1 ` `                    ``&& mp[s1] != i) { ` `                    ``string ans = words[i] + s1; ` `                    ``output.push_back(ans); ` `                ``} ` ` `  `                ``s1 = temp; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Return output ` `    ``return` `output; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``vector words; ` ` `  `    ``// Given array of words ` `    ``words = { ``"geekf"``, ``"geeks"``, ``"or"``, ` `              ``"keeg"``, ``"abc"``, ``"ba"` `}; ` ` `  `    ``// Function call ` `    ``vector result ` `        ``= palindromicPairs(words); ` ` `  `    ``// Print the palindromic strings ` `    ``// after combining them ` `    ``for` `(``auto` `x : result) { ` `        ``cout << x << endl; ` `    ``} ` ` `  `    ``return` `0; ` `}`

Output:

```geekfkeeg
geekskeeg
abcba
```

Time Complexity: O(N)
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 Check out this Author's contributed articles.

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.