Data Structures & AlgorithmsHardProduct Company SDE · string DP

Problem: What is the minimum number of insertions and deletions needed to turn 'abc' into 'adc'? (Only insertions and deletions are allowed, no substitutions.) Use the LCS-based formula len(a) + len(b) - 2 LCS, with LCS('abc','adc') = 2.

Problem: What is the minimum number of insertions and deletions needed to turn 'abc' into 'adc'? (Only insertions and deletions are allowed, no substitutions.) Use the LCS-based formula len(a) + len(b) - 2 * LCS, with LCS('abc','adc') = 2.
a, b = 'abc', 'adc'
lcs = 2
print(len(a) + len(b) - 2 * lcs)
What is printed?
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 4

Correct answer: A. 2

Delete b and insert d (2 operations) keeps the common subsequence 'ac'. A substitution would be a single operation, so this case differs from plain edit distance.

More Coding Interview Problems questions

Exams that ask this topic