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:
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 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 index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Longest prefix which is also suffix
- Check if suffix and prefix of a string are palindromes
- 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 a Palindromic String can be formed by concatenating Substrings of two given Strings
- Suffix Tree Application 4 - Build Linear Time Suffix Array
- Find longest palindrome formed by removing or shuffling chars from string
- Longest palindromic string possible by concatenating strings from a given array
- Longest string in an array which matches with prefix of the given string
- Longest string which is prefix string of at least two strings
- Suffix Tree Application 3 - Longest Repeated Substring
- Suffix Tree Application 5 - Longest Common Substring
- Suffix Tree Application 6 - Longest Palindromic Substring
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
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.