Predict the output
What does this print?
a, b = "abcde", "ace"
row = [0] * (len(b) + 1)
for x in a:
nxt = [0]
for j, y in enumerate(b, 1):
same = x == y
nxt.append(row[j - 1] + 1 if same else max(row[j], nxt[j - 1]))
row = nxt
print(row)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [0, 1, 2, 3]
Right: all of 'ace' appears in order inside 'abcde'.
The last row tells you the best for each prefix of the shorter string, so the final cell is the full answer.
Why not the others?
- B [0, 1, 1, 1]. Later letters c and e also match.
- C [0, 1, 2, 2]. e is the last letter of both strings and matches.