Given a string str consisting of lowercase English letters, the task is to find the longest palindromic string T which satisfies the following condition:
- T = p + m + s where p and s are the prefix and the suffix of the given string str respectively and the string m is either the prefix or suffix of the string str after removing both p and s from it.
- The string formed by the concatenation of p and s is a palindrome itself.
- Either of the strings p and s can be an empty string.
Input: str = “abcdfdcecba”
Here, p = “abc”
s = “cba”
m = “dfd”
p + s = “abccba” which is a palindrome and m = “dfd” is the prefix after removing the prefix and suffix from the string str. Therefore, T = “abcdfdcba”.
Input: str = “geeksforgeeks”
Here, p = “”
s = “g”
m = “”
p + s = “” which is a palindrome and m = “g” is the prefix after removing the prefix and suffix from the string str. Therefore, T = “g”.
Approach: The idea for this problem is to divide the answer into three parts, such that there will be a part of suffix and prefix of the given string which forms palindrome together which will form the beginning and the ending of the answer string. Now, after removing these prefix and suffix from the given string, we can find the maximum lengthed suffix or prefix string (which we may call midPalindrome) which is palindromic.
Therefore, the answer string will be given by:
answer = prefix + midPalindrome + suffix
The following steps can be followed to compute the answer to the problem:
- Find the length up to which the suffix and prefix of str form a palindrome together.
- Remove the suffix and prefix substrings which already forms a palindrome from str and store them in separate strings.
- Check all prefix and suffix substrings in the remaining string str and find the longest of such strings.
- Finally, combining all the three parts of the answer and return it.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Longest palindrome formed by concatenating and reordering strings of equal length
- 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
- Find longest palindrome formed by removing or shuffling chars from string
- Longest prefix which is also suffix
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- String from prefix and suffix of given two strings
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if suffix and prefix of a string are palindromes
- Longest string in an array which matches with prefix of the given string
- Longest prefix in a string with highest frequency
- Print the longest palindromic prefix of a given string
- Longest string which is prefix string of at least two 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.