Two-dimensional prefix DP
Align Two Beacon Transcripts
01. The question
Two lowercase beacon transcripts may skip letters. Return the length of their longest shared subsequence, preserving order in both. Original alignment scenario; this is not a common substring.
Before revealing the idea, predict the result and describe what information you would need to track.
02. Worked example
Input"cabde"; second = "ace"
Output2
- The prefix c matches the c in the second transcript.
- Later e extends the shared subsequence "ce" to length 2.
- "ae" also has length 2; only the length is returned.
Try your code
Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.
Edit your test cases
Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.
Input contract & reference code
Example data: "cabde". Parameter: "ace". Numeric parameters arrive as strings; convert them with int(parameter) when needed.
The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.
Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.
Printed output
Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.
03. The idea
Try sketching your own approach first. What would you track, and why would it work?
Reveal the idea
- Give empty prefixes length 0.
- Matching endpoint letters extend the diagonal prefix answer by one.
- Otherwise inherit the larger of the upper and left prefix answers.
04. Watch it happen
Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.
Input & settings
Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.
Working state / what changes
Ready to begin
Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.
Python / why it changes
The line behind the step
def solve(data, parameter=''):
first, second = data, parameter
lengths = [[0] * (len(second) + 1) for _ in range(len(first) + 1)]
for row in range(1, len(first) + 1):
for column in range(1, len(second) + 1):
if first[row - 1] == second[column - 1]:
lengths[row][column] = lengths[row - 1][column - 1] + 1
else:
lengths[row][column] = max(lengths[row - 1][column], lengths[row][column - 1])
return lengths[-1][-1]How to use this walkthrough
Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.
05. Python solution
def solve(data, parameter=''):
first, second = data, parameter
lengths = [[0] * (len(second) + 1) for _ in range(len(first) + 1)]
for row in range(1, len(first) + 1):
for column in range(1, len(second) + 1):
if first[row - 1] == second[column - 1]:
lengths[row][column] = lengths[row - 1][column - 1] + 1
else:
lengths[row][column] = max(lengths[row - 1][column], lengths[row][column - 1])
return lengths[-1][-1]06. Cost & edge cases
- Time
- O(mn)
- Space
- O(mn)
- Either empty transcript gives 0.
- Repeated letters must respect both orders.
- Input is a JSON string; the second transcript is plain parameter text. Both use 0-10 lowercase letters.