''' This code continues exploring the KMP algorithm idea, assuming that the overlap function has been precomputed. It uses 2 index variables - i - for current position in T j -for current position in P ''' __author__ = "Marina Barsky, and Catherine Liu" T = 'tictictictactictictic' P = 'tictic' M = len(P) N = len(T) of_list = [0, 0, 0, 1, 2, 3] # manually precomputed overlap function # algorithm i = 0 #current position at which to compare character in T j = 0 #current position at which to compare character in P while i < N: #while current position in T is within bounds #loop while characters match while j < M and T[i] == P[j]: i = i + 1 j = j + 1 if j == M: print("Found match at position", (i - M)) of_prev = of_list[j-1] if of_prev == 0: # not even first character match - move to the next i, restart pattern i = i + 1 j = 0 else: #stay on the same character in T j = of_prev #skip characters in P according to the OF