Given string str, the task is to find the longest palindromic substring formed by the concatenation of the prefix and suffix of the given string str.
Input: str = “rombobinnimor”
The concatenation of string “rombob”(prefix) and “mor”(suffix) is “rombobmor” which is a palindromic string.
The concatenation of string “rom”(prefix) and “innimor”(suffix) is “rominnimor” which is a palindromic string.
But the length of “rominnimor” is greater than “rombobmor”.
Therefore, “rominnimor” is the required string.
Input: str = “geekinakeeg”
The concatenation of string “geek”(prefix) and “akeeg”(suffix) is “geekakeeg” which is a palindromic string.
The concatenation of string “geeki”(prefix) and “keeg”(suffix) is “geekigeek” which is a palindromic string.
But the length of “geekakeeg” is equals to “geekikeeg”.
Therefore, any of the above string is the required string.
Approach: The idea is to use KMP Algorithm to find the longest proper prefix which is a palindrome of the suffix of the given string str in O(N) time.
- Find the longest prefix(say s[0, l]) which is also a palindrome of the suffix(say s[n-l, n-1]) of the string str. Prefix and Suffix don’t overlap.
- Out of the remaining substring(s[l+1, n-l-1]), find the longest palindromic substring(say ans) which is either a suffix or prefix of the remaining string.
- The concatenation of s[0, l], ans and s[n-l, n-l-1] is the longest palindromic substring.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Longest palindromic String formed using concatenation of given strings in any order
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest prefix which is also suffix
- Print the longest palindromic prefix of a given string
- String from prefix and suffix of given two strings
- Check if suffix and prefix of a string are palindromes
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Count of pairs of strings whose concatenation forms a palindromic string
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must
- Make palindromic string non-palindromic by rearranging its letters
- Check if K palindromic strings can be formed from a given string
- Print all possible palindromic string formed using any pair of given 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 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.
Improved By : chitranayal