For instance, when the source string is kitten and the target string is sitting , the edit distance amounts to 3: Replace k with s in kitten , Replace e with i in kitten , Add g to the end of kitten . This can be computed using dynamic programming in Python as shown below: def edit_distance ( s : str , t : str ) - > int : rows = len ( s ) + 1 cols = len ( t ) + 1 dist = [ [ 0 for x in range ( cols ) ] for x in range ( rows ) ] # source prefixes can be transformed into empty string by dropping all characters for i in range ( 1 , rows ) : dist [ i ] [ 0 ] = i # target prefixes can be transformed into empty string by inserting every character for i in range ( 1 , cols ) : dist [ 0 ] [ i ] = i for col in range ( 1 , cols ) : for row in range ( 1 , rows ) : if s [ row - 1 ] == t [ col - 1 ] : cost = 0 else : cost = 1 dist [ row ] [ col ] = min ( dist [ row - 1 ] [ col ] + 1 , # deletion dist [ row ] [ col - 1 ] + 1 , # insertion dist [ row - 1 ] [ col - 1 ] + cost ) # substitution return dist [ row ] [ col ] print ( edit_distance ( "kitten" , "sitting" ) )