← Quiz: DP on Grids and Strings: all 10 questions
Quiz: DP on Grids and Strings · Question 4 of 10
What does this print? (code)
What does this print?
a, b = "abcd", "acbd"
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):
if a[i - 1] == b[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
print(dp[-1][-1])
- A.4
- B.2
- C.3
- D.1
Answer & explanation
Correct answer: 3
"abd" (or "acd") appears in both in order; all four letters cannot, because b and c are swapped.
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 →