1. Gyanastra
  2. Algorithms
  3. Longest Common Subsequence
  4. Longest Common Subsequence: Subsequence vs Substring
Free lesson, no account needed 5 min read 9 practice questions after it
Asked in past papers5 times
  • GATE CSE2014
  • UGC NET2018201720152013

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:

Index12345678
CharacterRAVINDRA
  1. 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.
  2. 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 nn: each character has 2 possibilities (included or left out), so the total is 2×2×…2 \times 2 \times \dots (nn times) =2n= 2^n.
  • For the example, n=8n = 8, so there are 282^8 subsequences.
  • In general, for a string of length nn (also stated as mm), the number of subsequences is 2n2^n.

Worked examples

String: R A V I N D R A (length 8).

StepChosen indicesResultType
11, 2, 3, 4R A V ISubsequence; also a substring because contiguous
21, 3, 5, 8R V N ASubsequence; skipped 2, 4, 6, 7
31, 3, 5, 7R V N RSubsequence
42, 7A RSubsequence
51, 2, 5, 6, 8R A N D ASubsequence
6—Count of subsequences8 characters, each included or left out → 282^8 subsequences

Comparison

FeatureSubstringSubsequence
ContiguityMust be contiguousNeed not be contiguous; gaps allowed
Selection ruleConsecutive indices onlyAny set of indices in increasing order
Example (RAVINDRA)RAVI, VINRAVI, RVNA (1,3,5,8), AR (2,7), RANDA (1,2,5,6,8)
Empty stringLength-0 substring exists—
Count for length nn—2n2^n

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 nn is 2n2^n (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
Pages 1–2 of 2