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.
Input: arr = [“geekf”, “geeks”, “or”, “keeg”, “abc”, “ba”]
Output: [“geekfkeeg”, “geekskeeg”, “abcba”]
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”]
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:
- Store all the words in a Map with words as keys and indices as values.
- For each word(say str) in the arr break the string into string s1 and s2 such that s1 + s2 = str.
- 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).
- Repeat the above steps for all the strings in the array.
Below is the implementation of the above approach:
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.
- Longest palindromic String formed using concatenation of given strings in any order
- All possible strings of any length that can be formed from a given string
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Given a string, print all possible palindromic partitions
- Largest palindromic string possible from given strings by rearranging the characters
- Longest palindromic string possible by concatenating strings from a given array
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Pair with given product | Set 1 (Find if any pair exists)
- Queries to check if any pair exists in an array having values at most equal to the given pair
- Strings formed from given characters without any consecutive repeating characters
- Make palindromic string non-palindromic by rearranging its letters
- Find all distinct palindromic sub-strings of a given string
- Find all palindromic sub-strings of a given string | Set 2
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Length of array pair formed where one contains all distinct elements and other all same elements
- Distinct palindromic sub-strings of the given string using Dynamic Programming
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.
Improved By : chitranayal