medium2-D Dynamic ProgrammingMicrosoftAmazon
Shared Script Finder
A DNA analyst finds the longest common ordered match between two strands (letters need not be contiguous).
Given two strings, return the length of their longest common subsequence (not substring).
Choose approach
0
0
0
0
0
1
cur
?
?
0
?
?
?
0
?
?
?
0
?
?
?
0
?
?
?
1/15
Narrationstep 1 of 15
(a vs a) ✓ match → dp[1][1] = 1.
Variables
i=1
j=1
char1=a
char2=a
value=1
How to think about it
- Diagonal transition on match; L-shape max on mismatch.
- Reconstruct via backtrack from dp[m][n].
Edge cases to catch in interview
- Empty string -> 0.
- Identical strings -> length.
Examples
Example 1
0{"text1":"abc","text2":"def"}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed