Longest Possible Chunked Palindrome | C # program to find length of longest palindromic chunk ; Here s is the string whose LCP is needed ln is length of string evaluated till now and str is original string ; if there is noting at all ! ! ; if a single letter is left out ; for each length of substring chunk in string ; if left side chunk and right side chunk are same ; Call LCP for the part between the chunks and add 2 to the result . Length of string evaluated till now is increased by ( i + 1 ) * 2 ; Wrapper over LPCRec ( ) ; Driver Code
using System ; class GFG { private static int LPCRec ( string curr_str , int count , int len , string str ) { if ( string . ReferenceEquals ( curr_str , null ) curr_str . Length == 0 ) { return ( 0 ) ; } if ( curr_str . Length <= 1 ) { if ( count != 0 && str . Length - len <= 1 ) { return ( count + 1 ) ; } else { return ( 1 ) ; } } int n = curr_str . Length ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( curr_str . Substring ( 0 , i + 1 ) . Equals ( curr_str . Substring ( n - 1 - i , n - ( n - 1 - i ) ) ) ) { return LPCRec ( curr_str . Substring ( i + 1 , ( n - 1 - i ) - ( i + 1 ) ) , count + 2 , len + ( i + 1 ) * 2 , str ) ; } } return count + 1 ; } public static int LPC ( string str ) { return LPCRec ( str , 0 , 0 , str ) ; } public static void Main ( string [ ] args ) { Console . WriteLine ( " V ▁ : ▁ " + LPC ( " V " ) ) ; Console . WriteLine ( " VOLVO ▁ : ▁ " + LPC ( " VOLVO " ) ) ; Console . WriteLine ( " VOLVOV ▁ : ▁ " + LPC ( " VOLVOV " ) ) ; Console . WriteLine ( " ghiabcdefhelloadamhelloabcdefghi ▁ : ▁ " + LPC ( " ghiabcdefhelloadamhelloabcdefghi " ) ) ; Console . WriteLine ( " ghiabcdefhelloadamhelloabcdefghik ▁ : ▁ " + LPC ( " ghiabcdefhelloadamhelloabcdefghik " ) ) ; Console . WriteLine ( " antaprezatepzapreanta ▁ : ▁ " + LPC ( " antaprezatepzapreanta " ) ) ; } }