Print the longest palindromic prefix of a given string | Function to find the longest prefix which is palindrome ; Find the length of the given string ; For storing the length of longest Prefix Palindrome ; Loop to check the substring of all length from 1 to n which is palindrome ; String of length i ; To store the value of temp ; Reversing the value of temp ; If string temp is palindromic then update the length ; Print the palindromic string of max_len ; Driver code ; Function call
def LongestPalindromicPrefix ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE max_len = 0 NEW_LINE for length in range ( 0 , n + 1 ) : NEW_LINE INDENT temp = string [ 0 : length ] NEW_LINE temp2 = temp NEW_LINE temp3 = temp2 [ : : - 1 ] NEW_LINE if temp == temp3 : NEW_LINE INDENT max_len = length NEW_LINE DEDENT DEDENT print ( string [ 0 : max_len ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " abaac " ; NEW_LINE LongestPalindromicPrefix ( string ) NEW_LINE DEDENT