Given a string, find the longest substring which is palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”.
Structure of Palindromic Tree :
Palindromic Tree’s actual structure is close to directed graph. It is actually a merged structure of two Trees which share some common nodes(see the figure below for better understanding). Tree nodes store palindromic substrings of given string by storing their indices.
This tree consists of two types of edges :
1. Insertion edge (weighted edge)
2. Maximum Palindromic Suffix (un-weighted)
Insertion Edge :
Insertion edge from a node u to v with some weight x means that the node v is formed by inserting x at the front and end of the string at u. As ‘u’ is already a palindrome, hence the resulting string at node v will also be a palindrome. x will be a single character for every edge. Therefore, a node can have max 26 insertion edges (considering lower letter string).
Maximum Palindromic Suffix Edge :
As the name itself indicates that for a node this edge will point to its Maximum Palindromic Suffix String node. We will not be considering the complete string itself as the Maximum Palindromic Suffix as this will make no sense(self loops). For simplicity purpose, we will call it as Suffix edge(by which we mean maximum suffix except the complete string). It is quite obvious that every node will have only 1 Suffix Edge as we will not store duplicate strings in the tree.
We will create all the palindromic substrings and then return the last one we got, since that would be the longest palindromic substring so far.
Since a Palindromic Tree stores the palindromes in order of arrival of a certain character, so the Longest will always be at the last index of our tree array.
Below is the implementation of 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.
- Suffix Tree Application 6 - Longest Palindromic Substring
- Minimum length of substring whose rotation generates a palindromic substring
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Longest palindromic string possible after removal of a substring
- Rearrange string to obtain Longest Palindromic Substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest substring with K unique characters using Binary Search
- Longest substring consisting of vowels using Binary Search
- Maximum length palindromic substring such that it starts and ends with given char
- Shortest Palindromic Substring
- Minimum size substring to be removed to make a given string palindromic
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Longest palindromic String formed using concatenation of given strings in any order
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
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.