Skip to content
Mad4Study

Engineering & Science

GATE Computer Science

  • 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.

Fill the DP table c[i][j]=c[i−1][j−1]+1c[i][j] = c[i-1][j-1] + 1 when characters match, else max⁡(c[i−1][j],c[i][j−1])\max(c[i-1][j], c[i][j-1]). The final entry is 44.

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

  • 33 is too short — BCBA already has length 4.
  • 55 and 66 exceed the true maximum; the DP table never reaches 55.

Report an error

GATE CS · Algorithms · Algorithm Design