Help me figure out what's wrong with the Python code below. It's supposed to count the number of characters that overlap between the beginning of one string and the end of another. overlap('CTAAGT', 'TTCA') should return 1, but instead it returns 2.

def overlap(string1, string2):
    if len(string2) < len(string1):
        temp = string1
        string1 = string2
        string2 = temp

    print("string1: " + string1)
    print("string2: " + string2)
    
    for i in range(0, len(string1) + 1):
        print(f'{string1[i:]} == {string2[:(len(string1) - i)]}')
        if (string1[i:] == string2[:(len(string1) - i)]):
            break

    for j in range(0, len(string1) + 1):
        print(f'{string2[j:]} == {string1[:(len(string2) - j)]}')
        if (string2[j:] == string1[:(len(string2) - j)]):
            break

    return max(len(string1) - i, len(string2) - j)
The second for loop ends too early before it checks every position in the longer string to see if an overlap starts there. If you replace the len(string1) with len(string2) in the range of the for loop, it will continue iterating until it finds the real overlap value. Here's the updated code:

def overlap(string1, string2):
    if len(string2) < len(string1):
        temp = string1
        string1 = string2
        string2 = temp

    print("string1: " + string1)
    print("string2: " + string2)
    
    for i in range(0, len(string1) + 1):
        print(f'{string1[i:]} == {string2[:(len(string1) - i)]}')
        if (string1[i:] == string2[:(len(string1) - i)]):
            break

    for j in range(0, len(string2) + 1): # CHANGE: len(string1) to len(string2)
        print(f'{string2[j:]} == {string1[:(len(string2) - j)]}')
        if (string2[j:] == string1[:(len(string2) - j)]):
            break

    return max(len(string1) - i, len(string2) - j)