human
Find minimum shift for longest common prefix | function for KMP search.
preprocessing of longest proper prefix.
find out the longest prefix and position.
for new position with longer prefix in str2 update pos and Len.
prresult.
Driver Code"
Solution in Python:
 ... Give me a code to find minimum shift for longest common prefix and the other steps required?
assistant
#Here is code to find minimum shift for longest common prefix and the other steps required.
 def KMP ( m , n , str2 , str1 ) :
	pos = 0
	Len = 0
	p = [ 0 for i in range ( m + 1 ) ]
	k = 0
	for i in range ( 2 , n + 1 ) :
		while ( k > 0 and str1 [ k ] != str1 [ i - 1 ] ) :
			k = p [ k ]
		if ( str1 [ k ] == str1 [ i - 1 ] ) :
			k += 1
		p [ i ] = k
	j = 0
	for i in range ( m ) :
		while ( j > 0 and j < n and str1 [ j ] != str2 [ i ] ) :
			j = p [ j ]
		if ( j < n and str1 [ j ] == str2 [ i ] ) :
			j += 1
		if ( j > Len ) :
			Len = j
			pos = i - j + 1
	print ( " Shift ▁ = ▁ " , pos )
	print ( " Prefix ▁ = ▁ " , str1 [ : Len ] )
str1 = " geeksforgeeks "
str2 = " forgeeksgeeks "
n = len ( str1 )
str2 = str2 + str2
KMP ( 2 * n , n , str2 , str1 )