ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def lcs(a, b):
    dp = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
    for i in range(1, len(a) + 1):
        for j in range(1, len(b) + 1):
            dp[i][j] = dp[i - 1][j - 1] + 1 if a[i - 1] == b[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
    return dp[-1][-1]
print(lcs('abcde', 'ace'))
  1. A. 5
  2. B. 3
  3. C. 2
  4. D. 4

Correct answer: B. 3

The longest common subsequence of abcde and ace is ace, which has length 3.

More Python Programming questions

Exams that ask this topic