Sub topic 1
What Is a Subsequence?
The longest common subsequence problem depends on one definition: what counts as a subsequence. This chapter pins that definition down, separates it from the related idea of a substring, and gives you a small checker you can run.
Prerequisites
This chapter assumes you know:
- Basic Python syntax, including lists, loops, and functions
- Recursion and base cases
- Big-O notation for time and space
- Two-dimensional arrays or lists of lists
This chapter uses only the first item. The others matter in later chapters.
The Definition
A subsequence of a sequence t is any sequence you get by deleting zero or more elements of t and keeping the rest in their original order. You may skip elements, but you may not reorder them.
Take t = "ABCDE":
"ACE"is a subsequence. DeleteBandD."ABCDE"is a subsequence of itself. Delete nothing.""(the empty string) is a subsequence. Delete everything."AEC"is not a subsequence.Ccomes beforeEint, so keeping both cannot produceEfirst.
Another way to say the same thing: a subsequence is a choice of strictly increasing indices i1 < i2 < ... < ik into t, read off in that order.
Subsequence vs. Substring
A substring is a subsequence whose kept elements are contiguous, meaning no gaps are allowed. Every substring is a subsequence. The reverse is not true.
| Candidate | Subsequence of "ABCDE"? | Substring of "ABCDE"? |
|---|---|---|
| "BCD" | Yes | Yes |
| "ACE" | Yes | No (gaps) |
| "AEC" | No (out of order) | No |
| "" | Yes | Yes |
Checking and Enumerating Subsequences
To test whether s is a subsequence of t, scan t once and advance a pointer into s each time the characters match. Matching each character of s at its earliest possible position is always safe, because it leaves the most room for the characters that follow. The check runs in O(len(t)) time.
from itertools import combinations
def is_subsequence(s, t):
i = 0
for ch in t:
if i < len(s) and s[i] == ch:
i += 1
return i == len(s)
def all_subsequences(t):
return ["".join(c) for k in range(len(t) + 1) for c in combinations(t, k)]
print(is_subsequence("ACE", "ABCDE")) # True
print(is_subsequence("AEC", "ABCDE")) # False
print("ACE" in "ABCDE") # False: substring test
print("BCD" in "ABCDE") # True
subs = all_subsequences("ABC")
print(len(subs), subs) # 8 ['', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']Each element is either kept or deleted, so a sequence of length n has 2^n index choices. When characters repeat, some of those choices produce the same string. In "AAB", for example, two different index choices both spell "AB". The 2^n count will matter when we look at brute force later.
Common Mistakes
- Allowing reordering. A subsequence keeps the original order.
"AEC"fails even though every letter appears int. - Requiring contiguity. That is the substring rule. A subsequence may have gaps.
- Forgetting the empty sequence. It is a subsequence of everything, and it becomes the base case of the recurrence later on.
Exercise
Is "ACDB" a subsequence of "ABCBDAB"? Is it a substring?
Show answer
It is a subsequence: A at index 0, C at index 2, D at index 4, B at index 6, and the indices increase. It is not a substring, because those positions are not contiguous and "ACDB" never appears as a consecutive run.
Exercise
How many index choices give a subsequence of a 20-character string, counting the empty one? Why might the number of distinct subsequence strings be smaller?
Show answer
2^20 = 1,048,576, since each of the 20 positions is either kept or deleted. If characters repeat, different index choices can spell the same string, so there are fewer distinct strings.