python实现Held-Karp算法

想问想一下python怎么实现Help-Karp算法的代码


def help_karp(text, pattern):
    m = len(pattern)
    n = len(text)
    result = []
    i = 0
    j = 0
    while i < n:
        if text[i] == pattern[j]:
            i += 1
            j += 1
            if j == m:
                result.append(i - j)
                j = 0
        else:
            if j > 0:
                j = j - 1
                i = i - j + 1
            else:
                i += 1
    return result

在这个函数中,text是要搜索的文本,pattern是要匹配的模式。函数返回一个列表,其中包含所有匹配的位置。