I am reading about KMP for string matching. It needs a preprocessing of the pattern by building a prefix table. For example for the string ababaca
KMP
ababaca
p='ababaca' l1 = len(p) j = 0 i = 1 prefix = [0] while len(prefix) < l1: if p[j] == p[i]: prefix.append(j+1) i += 1 j += 1 else: if j == 0: prefix.append(0) i += 1 if j != 0: j = prefix[j-1] print prefix