Longest Possible Chunked Palindrome | Here curr_str is the string whose LCP is needed leng is length of string evaluated till now and s is original string ; If there is nothing 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
def LPCRec ( curr_str , count , leng , s ) : NEW_LINE INDENT if not curr_str : NEW_LINE INDENT return 0 NEW_LINE DEDENT if len ( curr_str ) <= 1 : NEW_LINE INDENT if count != 0 and len ( s ) - leng <= 1 : NEW_LINE INDENT return ( count + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT n = len ( curr_str ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT if ( curr_str [ 0 : i + 1 ] == curr_str [ n - 1 - i : n ] ) : NEW_LINE INDENT return LPCRec ( curr_str [ i + 1 : n - 1 - i ] , count + 2 , leng + ( i + 1 ) * 2 , s ) NEW_LINE DEDENT DEDENT return count + 1 NEW_LINE DEDENT def LPC ( s ) : NEW_LINE INDENT return LPCRec ( s , 0 , 0 , s ) NEW_LINE DEDENT print ( " V ▁ : " , LPC ( " V " ) ) NEW_LINE print ( " VOLVO ▁ : " , LPC ( " VOLVO " ) ) NEW_LINE print ( " VOLVOV ▁ : " , LPC ( " VOLVOV " ) ) NEW_LINE print ( " ghiabcdefhelloadamhelloabcdefghi ▁ : " , LPC ( " ghiabcdefhelloadamhelloabcdefghi " ) ) NEW_LINE print ( " ghiabcdefhelloadamhelloabcdefghik ▁ : " , LPC ( " ghiabcdefhelloadamhelloabcdefghik " ) ) NEW_LINE print ( " antaprezatepzapreanta ▁ : " , LPC ( " antaprezatepzapreanta " ) ) NEW_LINE