A subsequence is any sequence you can create by deleting but not rearranging any characters in a string.
So from “turtledoves” we can produce “loves.”
A substring is any sequence you can create by deleting characters off the beginning and ending of a string only.
Thus whereas “loves” is invalid as a substring, “doves” is valid.
Substrings are a subset of subsequences, and both are subsets of the powerset of the string’s characters. (This is why the naive algorithm of longest common subsequence is so expensive: you end up generating a powerset, which is an O(2^n) operation!)
I like LCS because there’s a graphical intuition you can keep in mind, and it helps distinguish the two algorithms while reinforcing their distinctions. I haven’t seen it written out elsewhere, so I wanted to make a note of it.
Here’s the matrix (“program”) formed for a Longest Common Subsequence:
The longest common subsequence is “ACAD” of length 4. (The utmost bottom right value.)
You can see the cascading effect even if you don’t recognize the algorithm. The matrix for these two strings in the substring matrix is very sparse in comparison:
Let’s change the strings to be a bit more overlapping.
Now we can see an aggregation again. Both algorithms rely on a core shape:
But subsequences are “looser” and thus make use of extra state. This state looks like this:
For me, these shapes are nice to remember in motivating the pure index equations of LCS[i-1][j-1] and max(LCS[i-1][j], LCS[i][j-1]).