human
Optimized Naive Algorithm for Pattern Searching | A modified Naive Pattern Searching algorithm that is optimized for the cases when all characters of pattern are different.
For current index i , check for pattern match.
if j == M : if pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ].
slide the pattern by j.
Driver program to test the above function"
How can the above be solved in Python?
assistant
#Here is code to optimized Naive Algorithm for Pattern Searching and the other steps required.
 def search ( pat , txt ) :
	M = len ( pat )
	N = len ( txt )
	i = 0
	while i <= N - M :
		for j in xrange ( M ) :
			if txt [ i + j ] != pat [ j ] :
				break
			j += 1
			print " Pattern ▁ found ▁ at ▁ index ▁ " + str ( i )
			i = i + M
		elif j == 0 :
			i = i + 1
		else :
			i = i + j
txt = " ABCEABCDABCEABCD "
pat = " ABCD "
search ( pat , txt )