Given a 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 palindromic String formed using concatenation of given strings in any order
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Print the longest prefix of the given string which is also the suffix of the same string
- 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
- Longest string in an array which matches with prefix of the given string
- Longest string which is prefix string of at least two strings
- Check if K palindromic strings can be formed from a given string
- Longest prefix which is also suffix
- Longest prefix in a string with highest frequency
- Length of longest palindromic sub-string : Recursion
- Longest palindromic string possible after removal of a substring
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Longest substring of 0s in a string formed by k concatenations
- Find longest palindrome formed by removing or shuffling chars from string
- Find the count of palindromic sub-string of a string in its sorted form
- Lexicographically smallest string formed by appending a character from first K characters of a 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.