Print the longest palindromic prefix of a given string | Function to find the longest prefix which is palindromic ; 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 reversed of temp ; Reversing String temp2 ; If String temp is palindromic then update the length ; Print the palindromic String of max_len ; Given String ; Function Call
function LongestPalindromicPrefix ( s ) { let n = s . length ; let max_len = 0 ; for ( let len = 1 ; len <= n ; len ++ ) { let temp = s . substring ( 0 , len ) ; let temp2 = temp ; temp2 = reverse ( temp2 ) ; if ( temp == temp2 ) { max_len = len ; } } document . write ( s . substring ( 0 , max_len ) ) ; } function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } let str = " " ; LongestPalindromicPrefix ( str ) ;