Data Structures & AlgorithmsHardProduct Company SDE pattern

Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed?

Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed?
def ed(a, b):
    prev = list(range(len(b) + 1))
    for i, x in enumerate(a, 1):
        cur = [i]
        for j, y in enumerate(b, 1):
            cur.append(prev[j-1] if x == y else 1 + min(prev[j-1], prev[j], cur[-1]))
        prev = cur
    return prev[-1]
print(ed("kitten", "sitting"), ed("intention", "execution"))
  1. A. 3 4
  2. B. 2 5
  3. C. 4 5
  4. D. 3 5

Correct answer: D. 3 5

kitten → sitten (k→s) → sittin (e→i) → sitting (+g) = 3. intention → execution needs 5 (one deletion, three substitutions and one insertion in the standard alignment). Two rolling rows make the space O(n). Time O(mn).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic