Longest Palindromic Substring using Palindromic Tree | Set 3 | Python3 code for Longest Palindromic substring using Palindromic Tree data structure ; store start and end indexes of current Node inclusively ; Stores length of substring ; stores insertion Node for all characters a - z ; stores the Maximum Palindromic Suffix Node for the current Node ; Stores Node information for constant time access ; Keeps track the Current Node while insertion ; Function to insert edge in tree ; Finding X , such that s [ currIndex ] + X + s [ currIndex ] is palindrome . ; Check if s [ currIndex ] + X + s [ currIndex ] is already Present in tree . ; Else Create new node ; Setting suffix edge for newly Created Node . ; Longest Palindromic suffix for a string of length 1 is a Null string . ; Else ; Driver code ; Imaginary root 's suffix edge points to   itself, since for an imaginary string   of length = -1 has an imaginary suffix   string. Imaginary root. ; NULL root 's suffix edge points to   Imaginary root, since for a string of   length = 0 has an imaginary suffix string.  ; last will be the index of our last substring
class Node : NEW_LINE INDENT def __init__ ( self , length = None , suffixEdge = None ) : NEW_LINE INDENT self . start = None NEW_LINE self . end = None NEW_LINE self . length = length NEW_LINE self . insertionEdge = [ 0 ] * 26 NEW_LINE self . suffixEdge = suffixEdge NEW_LINE DEDENT tree = [ Node ( ) for i in range ( MAXN ) ] NEW_LINE currNode , ptr = 1 , 2 NEW_LINE tree [ 1 ] = root1 NEW_LINE tree [ 2 ] = root2 NEW_LINE s = " forgeeksskeegfor " NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT insert ( i ) NEW_LINE DEDENT DEDENT def insert ( currIndex ) : NEW_LINE INDENT global currNode , ptr NEW_LINE temp = currNode NEW_LINE while True : NEW_LINE INDENT currLength = tree [ temp ] . length NEW_LINE if ( currIndex - currLength >= 1 and ( s [ currIndex ] == s [ currIndex - currLength - 1 ] ) ) : NEW_LINE INDENT break NEW_LINE DEDENT temp = tree [ temp ] . suffixEdge NEW_LINE DEDENT if tree [ temp ] . insertionEdge [ ord ( s [ currIndex ] ) - ord ( ' a ' ) ] != 0 : NEW_LINE INDENT currNode = tree [ temp ] . insertionEdge [ ord ( s [ currIndex ] ) - ord ( ' a ' ) ] NEW_LINE return NEW_LINE DEDENT ptr += 1 NEW_LINE tree [ temp ] . insertionEdge [ ord ( s [ currIndex ] ) - ord ( ' a ' ) ] = ptr NEW_LINE tree [ ptr ] . end = currIndex NEW_LINE tree [ ptr ] . length = tree [ temp ] . length + 2 NEW_LINE tree [ ptr ] . start = ( tree [ ptr ] . end - tree [ ptr ] . length + 1 ) NEW_LINE currNode = ptr NEW_LINE temp = tree [ temp ] . suffixEdge NEW_LINE if tree [ currNode ] . length == 1 : NEW_LINE INDENT tree [ currNode ] . suffixEdge = 2 NEW_LINE return NEW_LINE DEDENT while True : NEW_LINE INDENT currLength = tree [ temp ] . length NEW_LINE if ( currIndex - currLength >= 1 and s [ currIndex ] == s [ currIndex - currLength - 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT temp = tree [ temp ] . suffixEdge NEW_LINE DEDENT tree [ currNode ] . suffixEdge = tree [ temp ] . insertionEdge [ ord ( s [ currIndex ] ) - ord ( ' a ' ) ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT MAXN = 1000 NEW_LINE root1 = Node ( - 1 , 1 ) NEW_LINE root2 = Node ( 0 , 1 ) NEW_LINE last = ptr NEW_LINE for i in range ( tree [ last ] . start , tree [ last ] . end + 1 ) : NEW_LINE INDENT print ( s [ i ] , end = " " ) NEW_LINE DEDENT DEDENT