Algorithms · Longest Common Subsequence
Longest Common Subsequence: Subsequence vs Substring
Overview
A subsequence is formed by picking characters at increasing indices and skipping any characters in between, while a substring must be a contiguous block with no skipped characters.
Distinguishing the two is essential because the Longest Common Subsequence problem allows gaps, and subsequence counting and comparison have direct applications such as measuring DNA similarity between species.
Key concepts
- Subsequence: obtained by choosing some indices of the string such that the chosen indices are in increasing order, then writing the characters at those indices. Characters in the middle may be skipped.
- Substring: must be contiguous — characters in between cannot be skipped.
- Rule for subsequences: as long as the chosen indices are in ascending order, the resulting string is a subsequence; any characters in the middle may be left out.
- Empty string: a substring of length 0 (the empty string) is also a substring.
- Containment: every substring is a subsequence, but not every subsequence is a substring.
- Applications: the subsequence problem is interesting because it has many applications; one common application is comparing DNA strands of different species to measure similarity.
How it works
Take the string R-A-V-I-N-D-R-A with indices:
| Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Character | R | A | V | I | N | D | R | A |
- Substrings (contiguous): RAVI (indices 1–4) is a substring; VIN (indices 3–5) is a substring; the empty string (length 0) is also a substring.
- Subsequences (indices in increasing order, gaps allowed): choose any set of indices in ascending order and write the characters at those indices, skipping the rest.
- Indices 1, 2, 3, 4 → R, A, V, I — a subsequence (also a substring here, since it is contiguous).
- Indices 1, 3, 5, 8 → indices are increasing, so it is a subsequence; here 2, 4, 6, 7 are skipped.
- Indices 1, 3, 5, 7 → R, V, N, R — a subsequence.
- Indices 2, 7 → A, R — a subsequence.
- Indices 1, 2, 5, 6, 8 → R, A, N, D, A — a subsequence.
Formulas
- Number of subsequences of a string of length : each character has 2 possibilities (included or left out), so the total is ( times) .
- For the example, , so there are subsequences.
- In general, for a string of length (also stated as ), the number of subsequences is .
Worked examples
String: R A V I N D R A (length 8).
| Step | Chosen indices | Result | Type |
|---|---|---|---|
| 1 | 1, 2, 3, 4 | R A V I | Subsequence; also a substring because contiguous |
| 2 | 1, 3, 5, 8 | R V N A | Subsequence; skipped 2, 4, 6, 7 |
| 3 | 1, 3, 5, 7 | R V N R | Subsequence |
| 4 | 2, 7 | A R | Subsequence |
| 5 | 1, 2, 5, 6, 8 | R A N D A | Subsequence |
| 6 | — | Count of subsequences | 8 characters, each included or left out → subsequences |
Comparison
| Feature | Substring | Subsequence |
|---|---|---|
| Contiguity | Must be contiguous | Need not be contiguous; gaps allowed |
| Selection rule | Consecutive indices only | Any set of indices in increasing order |
| Example (RAVINDRA) | RAVI, VIN | RAVI, RVNA (1,3,5,8), AR (2,7), RANDA (1,2,5,6,8) |
| Empty string | Length-0 substring exists | — |
| Count for length | — |
Revision summary
- Substring = contiguous block of characters;
subsequence = characters picked at increasing indices with gaps allowed. - Every substring is a subsequence, but not every subsequence is a substring.
- The number of subsequences of a string of length is (each character: take or skip).
- The empty string (length 0) counts as a substring.
- Subsequence comparison has many applications; one common one is comparing DNA strands of different species to measure similarity.
Previous-year exam insights
- The number of substrings of a string of length n (including the empty string) is n(n+1)/2 + 1; the number of non-empty substrings is n(n+1)/2.
Asked inGATE 2014UGC NET 2018UGC NET 2017UGC NET 2015UGC NET 2013 - LCS is computed by the DP recurrence: L[i][j] = 0 if i=0 or j=0;
L[i][j] = L[i-1][j-1] + 1 if X[i] = Y[j];
otherwise L[i][j] = max(L[i-1][j], L[i][j-1]).
The table is filled in O(mn) time and O(mn) space.
Asked inGATE 2014UGC NET 2018UGC NET 2017UGC NET 2015UGC NET 2013 - A common subsequence of X and Y must appear as a subsequence in both strings with the same relative order; the LCS need not be unique, and the number of distinct LCS strings can be counted by a DP that sums counts along optimal transitions.
Asked inGATE 2014 - The length of the longest common substring (contiguous) is computed by a different recurrence: L[i][j] = L[i-1][j-1] + 1 if X[i] = Y[j], else 0, and the answer is the maximum over all cells, not the last cell.
Asked inUGC NET 2018UGC NET 2017 - Any subsequence of a string of length n has length between 0 and n; the empty string is a subsequence of every string, and a string is a subsequence of itself.
Asked inUGC NET 2015 - LCS length satisfies LCS(X,Y) ≤ min(|X|,|Y|), and if X is a subsequence of Y then LCS(X,Y) = |X|.
Asked inUGC NET 2013UGC NET 2015