Length of Longest Palindrome Substring | Function to find the length of the longest palindromic subString ; Length of String str ; Stores the dp states ; All subStrings of length 1 are palindromes ; Check for sub - String of length 2 ; If adjacent character are same ; Update table [ i ] [ i + 1 ] ; Check for lengths greater than 2 k is length of subString ; Fix the starting index ; Ending index of subString of length k ; Check for palindromic subString str [ i , j ] ; Mark True ; Update the maximum length ; Return length of LPS ; Driver Code ; Given String str ; Function Call
def longestPalSubstr ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE table = [ [ False for i in range ( n ) ] for j in range ( n ) ] ; NEW_LINE maxLength = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT table [ i ] [ i ] = True ; NEW_LINE DEDENT start = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT table [ i ] [ i + 1 ] = True ; NEW_LINE start = i ; NEW_LINE maxLength = 2 ; NEW_LINE DEDENT DEDENT for k in range ( 3 , n + 1 ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT j = i + k - 1 ; NEW_LINE if ( table [ i + 1 ] [ j - 1 ] and str [ i ] == str [ j ] ) : NEW_LINE INDENT table [ i ] [ j ] = True ; NEW_LINE if ( k > maxLength ) : NEW_LINE INDENT start = i ; NEW_LINE maxLength = k ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return maxLength ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " forgeeksskeegfor " ; NEW_LINE print ( longestPalSubstr ( str ) ) ; NEW_LINE DEDENT