How can I determine all possible ways a subsequence can be removed from a sequence?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A subsequence removal can be interpreted as choosing indices in the original sequence that match a target subsequence and then deleting those indices. Different index choices can produce different removal "ways" even if the removed values look the same. Solving this well requires separating two tasks: counting all valid removals and enumerating the exact index combinations.
Problem Definition
Given sequence S and target subsequence T:
- A valid removal way is an increasing index list in
Swhose values formT. - Removing those indices produces a residual sequence.
Example:
S = "banana"T = "ban"
The first three letters give one valid removal. If repeated characters exist, multiple index sets may match same target.
Enumerating All Removal Ways
Backtracking is the clearest approach when you need explicit index combinations.
Output index tuples represent all distinct ways to remove T as subsequence.
Counting Without Listing All Ways
If you only need count, dynamic programming is much faster and memory-friendly than storing all index tuples.
Define dp[i][j] as number of ways target prefix length j appears as subsequence in source prefix length i.
Transition:
- Ignore current source character.
- If characters match, also include ways using that match.
This is the classic distinct-subsequence count formulation.
Residual Sequences After Removal
Sometimes question is not about index sets but resulting sequences after deletion. Different index sets may produce same residue, so deduplication may be needed.
This distinction matters in combinatorics tasks where unique outcomes are required.
Complexity Discussion
For enumeration:
- Runtime can grow with number of valid matches, which can be exponential in repeated-character cases.
- Memory grows with number of stored ways.
For DP counting:
- Time complexity is
O(len(S) * len(T)). - Space can be reduced to one dimension if only final count is needed.
Space-optimized counting:
Reverse update order is essential to avoid reusing updated values from same source character.
When to Use Which Method
Use enumeration when:
- You need explicit index paths.
- You need to reconstruct specific removals.
Use DP count when:
- You only need number of ways.
- Input size is larger and enumeration is infeasible.
Hybrid strategy can first count via DP, then enumerate only when count is manageable.
Common Pitfalls
- Confusing subsequence with substring. Fix by remembering subsequence allows gaps while preserving order.
- Counting value matches instead of index-based matches. Fix by defining removal way as index tuple.
- Forgetting duplicate residues from different index choices. Fix by deduplicating residual strings when required.
- Using forward one-dimensional DP update. Fix by updating target index in reverse order.
- Enumerating blindly on large repeated strings. Fix by using DP counting first to estimate search size.
Summary
- A removal way is best modeled as a strictly increasing index set matching target subsequence.
- Backtracking enumerates exact removal paths.
- Dynamic programming counts paths efficiently without full enumeration.
- Residual-sequence counting may require deduplication separate from path counting.
- Choose method by output requirement: explicit paths or just total count.

