def MotifCount(string1, substring, d):
    for i in range(len(string1) - len(substring) + 1):
        if HammingDistance(string1[i:i + len(substring)], substring) <= d:
            print(i)
MotifCount(
    'CGCCCGAATCCAGAACGCATTCCCATATTTCGGGACCACTGGCCTCCACGGTACGGACGTCAATCAAAT',
    'ATTCTGGA', 3)
How to optimize a python script which runs for 4**k times?