Maximum length palindromic substring for every index such that it starts and ends at that index | Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code
function isPalindrome ( S , i , j ) { while ( i < j ) { if ( S [ i ] !== S [ j ] ) return false ; i ++ ; j -- ; } return true ; } function printLongestPalindrome ( S , N ) { var palLength = new Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { var maxlength = 1 ; for ( var j = 0 ; j < i ; j ++ ) { if ( S [ j ] === S [ i ] ) { if ( isPalindrome ( S , j , i ) !== false ) { maxlength = i - j + 1 ; break ; } } } for ( var j = N - 1 ; j > i ; j -- ) { if ( S [ j ] === S [ i ] ) { if ( isPalindrome ( S , i , j ) ) { maxlength = Math . max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( var i = 0 ; i < N ; i ++ ) { document . write ( palLength [ i ] + " " ) ; } } var S = " " ; var N = S . length ; printLongestPalindrome ( S , N ) ;