- GATE CS
- 2026–27 pattern practice
- Algorithms
- Dynamic programming
- Medium
What is the length of the longest common subsequence (LCS) of "ABCBDAB" and "BDCABA"?
Algorithm Design
Pick an option to check your answer.
Answer: B. Solution
Fill the DP table when characters match, else . The final entry is .
One LCS is BCBA: B-C-B-A appears in order in ABCBDAB (positions 2, 3, 4, 6) and in BDCABA (positions 1, 3, 5, 6). Others of length 4 include BDAB and BCAB; none of length 5 exists.
Why not the others
- is too short — BCBA already has length 4.
- and exceed the true maximum; the DP table never reaches .