def kmp(sequence, sub):
    """         
    Knuth–Morris–Pratt algorithm, returning the starting position
    of a specified sub within another, larger sequence.
    Often used for string matching.
    """
    partial = [0]
    for i in range(1, len(sub)):
        j = partial[i - 1]
        while j > 0 and sub[j] != sub[i]:
            j = partial[j - 1]
        partial.append(j + 1 if sub[j] == sub[i] else j)

    positions, j = [], 0
    for i in range(len(sequence)):
        while j > 0 and sequence[i] != sub[j]:
            j = partial[j - 1]
        if sequence[i] == sub[j]: j += 1
        if j == len(sub): 
            positions.append(i - (j - 1))
            j = 0

    return positions