Longest Palindromic Substring | Set 1 | Function to pra subString str [ low . . high ] ; This function prints the longest palindrome subString It also returns the length of the longest palindrome ; Get length of input String ; All subStrings of length 1 are palindromes ; Nested loop to mark start and end index ; Check palindrome ; Palindrome ; Return length of LPS ; Driver Code
def printSubStr ( str , low , high ) : NEW_LINE INDENT for i in range ( low , high + 1 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT def longestPalSubstr ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE maxLength = 1 NEW_LINE start = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT flag = 1 NEW_LINE for k in range ( 0 , ( ( j - i ) // 2 ) + 1 ) : NEW_LINE INDENT if ( str [ i + k ] != str [ j - k ] ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag != 0 and ( j - i + 1 ) > maxLength ) : NEW_LINE INDENT start = i NEW_LINE maxLength = j - i + 1 NEW_LINE DEDENT DEDENT DEDENT print ( " Longest ▁ palindrome ▁ subString ▁ is : ▁ " , end = " " ) NEW_LINE printSubStr ( str , start , start + maxLength - 1 ) NEW_LINE return maxLength NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " forgeeksskeegfor " NEW_LINE print ( " Length is :   " , longestPalSubstr ( str ) ) NEW_LINE DEDENT