def edit_distance_table(source: str, target: str) -> list[list[int]]:
    """Return unit-cost edit distances for every pair of string prefixes.

    Args:
        source: Original string, interpreted as Unicode code points.
        target: Required string, interpreted as Unicode code points.

    Returns:
        A table whose entry [i][j] gives the minimum cost to transform
        source[:i] into target[:j].
    """
    table = [[0] * (len(target) + 1) for _ in range(len(source) + 1)]
    for row in range(len(source) + 1):
        table[row][0] = row
    for column in range(len(target) + 1):
        table[0][column] = column
    for row in range(1, len(source) + 1):
        for column in range(1, len(target) + 1):
            mismatch = int(source[row - 1] != target[column - 1])
            deletion = table[row - 1][column] + 1
            insertion = table[row][column - 1] + 1
            substitution = table[row - 1][column - 1] + mismatch
            table[row][column] = min(deletion, insertion, substitution)
    return table


source = "GAT"
target = "GCTT"
table = edit_distance_table(source, target)
prefixes = [target[:column] for column in range(len(target) + 1)]
print("Target prefixes:", prefixes)
for row, costs in enumerate(table):
    print("Source prefix", repr(source[:row]), "costs", costs)
print("Minimum edit cost:", table[-1][-1])

shared_rows = [[0] * 3] * 2
shared_rows[0][1] = 9
separate_rows = [[0] * 3 for _ in range(2)]
separate_rows[0][1] = 9
print("Repeated row reference:", shared_rows)
print("Independent rows:", separate_rows)
print("Repeated rows share identity:", shared_rows[0] is shared_rows[1])
print("Independent rows share identity:", separate_rows[0] is separate_rows[1])

def edit_distance(source: str, target: str) -> int:
    """Return unit-cost edit distance with storage for two short rows.

    Args:
        source: Original string, interpreted as Unicode code points.
        target: Required string, interpreted as Unicode code points.

    Returns:
        The minimum number of insertions, deletions, and substitutions.
    """
    if len(source) < len(target):
        source, target = target, source
    if not target:
        return len(source)
    previous = list(range(len(target) + 1))
    for row, source_character in enumerate(source, start=1):
        current = [row] + [0] * len(target)
        for column, target_character in enumerate(target, start=1):
            mismatch = int(source_character != target_character)
            current[column] = min(
                previous[column] + 1,
                current[column - 1] + 1,
                previous[column - 1] + mismatch,
            )
        previous = current
    return previous[-1]


print("Distance with two rows:", edit_distance(source, target))
print("Distance with arguments reversed:", edit_distance(target, source))
print("Distance from an empty string:", edit_distance("", target))
assert edit_distance(source, target) == table[-1][-1]

cases = [
    ("", ""),
    ("", "AC"),
    ("AC", ""),
    ("AAA", "AAA"),
    ("AB", "BA"),
    ("A", "TTTT"),
    ("é", "e"),
]
for first, second in cases:
    full_table = edit_distance_table(first, second)
    distance = edit_distance(first, second)
    assert distance == full_table[-1][-1]
    assert distance == edit_distance(second, first)
    assert abs(len(first) - len(second)) <= distance <= max(
        len(first), len(second)
    )
    print(repr(first), "to", repr(second), "cost", distance)
assert edit_distance("AB", "BA") == 2
assert edit_distance("AAA", "AAA") == 0
print("Boundary and operation checks passed.")
