MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / min_distance

Function min_distance

dynamic_programming/min_distance_up_bottom.py:29–41  ·  view source on GitHub ↗
(index1: int, index2: int)

Source from the content-addressed store, hash-verified

27
28 @functools.cache
29 def min_distance(index1: int, index2: int) -> int:
30 # if first word index overflows - delete all from the second word
31 if index1 >= len_word1:
32 return len_word2 - index2
33 # if second word index overflows - delete all from the first word
34 if index2 >= len_word2:
35 return len_word1 - index1
36 diff = int(word1[index1] != word2[index2]) # current letters not identical
37 return min(
38 1 + min_distance(index1 + 1, index2),
39 1 + min_distance(index1, index2 + 1),
40 diff + min_distance(index1 + 1, index2 + 1),
41 )
42
43 return min_distance(0, 0)
44

Callers 1

min_distance_up_bottomFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected