🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: DP on Grids and Strings: all 10 questions

Quiz: DP on Grids and Strings · Question 8 of 10

What does this print? (code)

What does this print?

def ed(a, b):
    dp = [[i + j if i * j == 0 else 0 for j in range(len(b) + 1)] for i in range(len(a) + 1)]
    for i in range(1, len(a) + 1):
        for j in range(1, len(b) + 1):
            if a[i - 1] == b[j - 1]:
                dp[i][j] = dp[i - 1][j - 1]
            else:
                dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
    return dp[-1][-1]
print(ed("cat", "cut"), ed("", "abc"))
  1. A.1 3
  2. B.2 3
  3. C.1 0
  4. D.0 3
Answer & explanation

Correct answer: 1 3

"cat" to "cut" is one replace; an empty string needs 3 inserts to become "abc".

Can you get the rest right without peeking?

Take the timed Quiz: DP on Grids and Strings quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →