Length of longest palindromic sub | Java implementation to find the length of longest palindromic sub - String using Recursion ; Function to find maximum of the two variables ; Function to find the longest palindromic subString : Recursion ; Base condition when the start index is greater than end index ; Base condition when both the start and end index are equal ; Condition when corner characters are equal in the String ; Recursive call to find the longest Palindromic String by excluding the corner characters ; Recursive call to find the longest Palindromic String by including one corner character at a time ; Function to find the longest palindromic sub - String ; Utility function call ; Driver Code ; Function Call
class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int longestPalindromic ( String str , int i , int j , int count ) { if ( i > j ) return count ; if ( i == j ) return ( count + 1 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) { count = longestPalindromic ( str , i + 1 , j - 1 , count + 2 ) ; return max ( count , max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ) ; } return Math . max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ; } static int longest_palindromic_substr ( String str ) { return longestPalindromic ( str , 0 , str . length ( ) - 1 , 0 ) ; } public static void main ( String [ ] args ) { String str = " aaaabbaa " ; System . out . print ( longest_palindromic_substr ( str ) ) ; } }