root@coding-prodigies:~#
← All challenges

Longest Common Subsequence

Classic dynamic programming: the longest sequence of letters both strings share, in order.

🔴 ExtremeDifficulty

The challenge

Given "ABCBDAB" and "BDCABA", find the length of their longest common subsequence -- the longest sequence of characters that appears in both strings in the same relative order (not necessarily contiguous) -- and print the length.

Output

  
🧠 Need a hint? (Python)

Build a 2D table dp where dp[i][j] is the LCS length of a[:i] and b[:j]. If a[i-1] == b[j-1], dp[i][j] = dp[i-1][j-1] + 1; otherwise dp[i][j] = max(dp[i-1][j], dp[i][j-1]).

🚀

Like solving these?

Structured courses, hands-on exercises, and real multi-file projects are waiting -- your first project unlock is free.

Create a free account

More challenges