VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 12: Dynamic programming

Lesson 12.11 of 16 in this module Unrolled Pro

Longest Common Subsequence

The length of the longest sequence that appears, in order but not necessarily contiguously, in both strings. From checking every subsequence of one string against the other (2^m), to the two-sequence DP table where matching characters extend the diagonal and mismatches take the better of dropping one character (O(m · n)), to two rolling rows, plus recovering the subsequence. Then a line-based diff, the shortest common supersequence, and ROUGE-L, the LCS-based metric for summaries.

about 10 min 3 Python examples 2 quick checks

Problem. Given strings a and b, return the length of their longest common subsequence (LCS): the longest sequence of characters that appears in both, in the same order, not necessarily contiguous.

text
a = "abcde",   b = "ace"     →   3      ("ace")
a = "abc",     b = "def"     →   0

This is the first two-sequence DP: the state is a pair (i, j), "the LCS of the first i characters of a and the first j of b". Compare the last characters: if they match, they can end the common subsequence; if not, at least one of them isn't in it, so drop one and take the better.

ML context. LCS measures ordered overlap between sequences. ROUGE-L, a standard summarisation metric, is an F-score built on the LCS between a generated summary and a reference (Change D). diff and version control show changes between files as an LCS of lines (Change A). Lesson 12.12's edit distance is a close relative used for word error rate.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Longest Common Subsequence"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee