human
Shortest Palindromic Substring | Function return the shortest palindromic substring.
One by one consider every character as center point of even and length palindromes.
Find the longest odd length palindrome with center point as i.
Find the even length palindrome with center points as i - 1 and i ..
Smallest substring which is not empty.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to shortest Palindromic Substring and the other steps required.
 def ShortestPalindrome ( s ) :
	n = len ( s )
	v = [ ]
	for i in range ( n ) :
		l = i
		r = i
		ans1 = " "
		ans2 = " "
		while ( ( l >= 0 ) and ( r < n ) and ( s [ l ] == s [ r ] ) ) :
			ans1 += s [ l ]
			l -= 1
			r += 1
		l = i - 1
		r = i
		while ( ( l >= 0 ) and ( r < n ) and ( s [ l ] == s [ r ] ) ) :
			ans2 += s [ l ]
			l -= 1
			r += 1
		v . append ( ans1 )
		v . append ( ans2 )
	ans = v [ 0 ]
	for i in range ( len ( v ) ) :
		if ( v [ i ] != " " ) :
			ans = min ( ans , v [ i ] )
	return ans
s = " geeksforgeeks "
print ( ShortestPalindrome ( s ) )