Common subsequence of given length
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In computer science and mathematics, the problem of finding a common subsequence of a given length among two or more sequences is a fascinating area with numerous applications, ranging from bioinformatics and text comparison to data compression and telecommunication. A subsequence is a sequence derived from another sequence by deleting some elements without changing the order of the remaining elements.
A common subsequence of given sequences is a subsequence that appears in the same order in all the sequences being considered, but not necessarily contiguously. Finding such a subsequence of a specified length is a more challenging task than the simpler problem of finding the longest common subsequence (LCS).
Problem Statement
Given two sequences, and , and a length, , the problem is to determine a subsequence of length that is common to both and . This involves checking whether such a subsequence exists and finding at least one example if it does.
Example:
Consider strings $A = \text\{"ABCBDAB"\}$ and $B = \text\{"BDCAB"\}$. A common subsequence of length 3 is "BCA", where characters appear in the same order but may not be contiguous.
Technical Explanation
Dynamic Programming Approach
To solve this problem, dynamic programming can be employed efficiently. Let's define a 3D array where:
• is the index in string , • is the index in string , • is the length of the desired subsequence.
The array will hold a boolean true if there is a common subsequence of length using the first characters of and the first characters of , otherwise false.
Initialization:
- for all and , because a subsequence of length 0 is trivially common.
$dp[0][j][l] = \text\{False\}$ and $dp[i][0][l] = \text\{False\}$ for $l > 0$, as there is no non-zero subsequence in a zero-length string.
Transition:
For , , and :
• If :
• Otherwise:
You should finally check the value of to determine if a common subsequence of length exists.
Complexity
• Time Complexity: where and are the lengths of sequences and , respectively. • Space Complexity: Also for storing the dynamic programming table.
Example Walkthrough
Consider the sequences $A = \text\{"AGGTAB"\}$ and $B = \text\{"GXTXAYB"\}$, trying to find a common subsequence of length 4.
Step-by-Step:
- Initialization:
• Genome Sequencing: Comparing DNA sequences to find common mutations or evolutionary paths. • Spell Checkers: Detecting differences between two texts for correction suggestions. • Data Compression: Identifying redundant sequences in data for compression.
Related reading
- Compare every item to every other item in ArrayList
- compare function for upper_bound / lower_bound
- Compare multiple algorithms with sklearn pipeline
- Compare string similarity
- Compare string similarity
- Compare two integer arrays with same length
- Compare two numbers for likeness
- Comparing 1 million integers in an array without sorting it first

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.