human
Can you provide a code to longest palindromic string possible after removal of a substring and the other steps required given the following:
Longest palindromic string possible after removal of a substring | Function to find the longest palindrome from the start of the string using KMP match.
Append S ( reverse of C ) to C.
Use KMP algorithm.
Function to return longest palindromic string possible from the given string after removal of any substring.
Initialize three strings A , B AND F.
Loop to find longest substrings from both ends which are reverse of each other.
Proceed to third step of our approach.
Remove the substrings A and B.
Find the longest palindromic substring from beginning of C.
Find the longest palindromic substring from end of C.
Store the maximum of D and E in F.
Find the final answer.
Driver code"
Solution in Python:
assistant
#Here is code to longest palindromic string possible after removal of a substring and the other steps required.
 def findPalindrome ( C ) :
	S = C [ : : - 1 ]
	C = C [ : ] + ' & ' + S
	n = len ( C )
	longestPalindrome = [ 0 for i in range ( n ) ]
	longestPalindrome [ 0 ] = 0
	ll = 0
	i = 1
	while ( i < n ) :
		if ( C [ i ] == C [ ll ] ) :
			ll += 1
			longestPalindrome [ i ] = ll
			i += 1
		else :
			if ( ll != 0 ) :
				ll = longestPalindrome [ ll - 1 ]
			else :
				longestPalindrome [ i ] = 0
				i += 1
	ans = C [ 0 : longestPalindrome [ n - 1 ] ]
	return ans
def findAns ( s ) :
	A = " "
	B = " "
	F = " "
	i = 0
	j = len ( s ) - 1
	ll = len ( s )
	while ( i < j and s [ i ] == s [ j ] ) :
		i = i + 1
		j = j - 1
	if ( i > 0 ) :
		A = s [ 0 : i ]
		B = s [ ll - i : ll ]
	if ( ll > 2 * i ) :
		C = s [ i : i + ( len ( s ) - 2 * i ) ]
		D = findPalindrome ( C )
		C = C [ : : - 1 ]
		E = findPalindrome ( C )
		if ( len ( D ) > len ( E ) ) :
			F = D
		else :
			F = E
	answer = A + F + B
	return answer
if __name__ == " _ _ main _ _ " :
	str = " abcdefghiedcba "
	print ( findAns ( str ) )