How to test if one string is a subsequence of another?
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
A string s is a subsequence of another string t if you can delete some characters from t without changing the order of the characters that remain and obtain s. The standard solution is a two-pointer scan that runs in linear time and is usually the best choice unless you need to answer many subsequence queries against the same large target string.
Use Two Pointers for the Normal Case
The simplest algorithm keeps one pointer in s and one in t:
- advance through
t - whenever characters match, advance in
s - if you reach the end of
s, it is a subsequence
Python example:
This works in O(len(t)) time and O(1) extra space, which is optimal for a single query when you must inspect the target string in order.
The logic is straightforward: characters in s must appear in t in the same order, but they do not need to be adjacent.
Walk Through an Example
Take:
- '
s = "abc"' - '
t = "ahbgdc"'
The algorithm compares:
- '
'a'with characters intuntil it finds'a'' - then
'b'until it finds'b' - then
'c'until it finds'c'
Since all three characters are found in order, "abc" is a subsequence of "ahbgdc".
For "axc" against the same t, the scan never finds 'x', so the result is false.
This is why the two-pointer solution is both easy to reason about and efficient.
Handle Edge Cases Explicitly
A few special cases are worth keeping in mind:
- the empty string is a subsequence of every string
- a non-empty string is never a subsequence of an empty string
- if
sis longer thant, the answer is automatically false
You can incorporate some of those checks early:
This version can stop early as soon as the last required character is matched.
Optimize Only If You Have Many Queries
If you need to check thousands or millions of candidate strings against the same target string t, the simple scan may become too slow overall. In that situation, preprocess t so you can jump to the next occurrence of each character quickly.
One approach is to build an index of character positions:
This is more complex, so it is worth it only when the same t is reused for many subsequence tests.
For one-off checks, the two-pointer method is still better.
Common Pitfalls
The biggest mistake is confusing subsequence with substring. A subsequence allows gaps; a substring requires contiguity.
Another issue is overcomplicating the solution for a single query. The two-pointer scan is already optimal enough for normal use.
Developers also sometimes forget the empty-string case. By definition, the empty string is always a subsequence.
Finally, if you optimize for many queries with a precomputed index, make sure you only pay that preprocessing cost when the same target string is reused often enough to justify it.
Summary
- The standard way to test subsequence status is a two-pointer scan.
- It runs in linear time in the length of the target string and uses constant extra space.
- A subsequence preserves order but does not require adjacent characters.
- The empty string is always a subsequence.
- More advanced indexing approaches only make sense when you must test many strings against the same target.
Related reading
- How to think in recursive way?
- How to trace the path in a Breadth-First Search?
- How to traverse a tree from sklearn AgglomerativeClustering?
- How to traverse cyclic directed graphs with modified DFS algorithm
- How to turn integers into Fibonacci coding efficiently?
- How to understand Locality Sensitive Hashing?
- How to understand the dynamic programming solution in linear partitioning?
- How to understand the knapsack problem is NP-complete?

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.