Is Subsequence
Problem Statement
You are given two strings s and t.
Return true if s is a subsequence of t, otherwise false.
s is a subsequence of t if you can get s by deleting some characters of t (maybe none) without changing the order of the characters that remain. For example, "ace" is a subsequence of "abcde", but "aec" is not.
Follow-up: suppose there are a huge number of strings s1, s2, β¦, sk (say a billion) and you must check each one against the same t. How would you change your approach?
Two strings s and t of lowercase English letters.
true if s is a subsequence of t, else false.
Delete h, g and d from t: aΒ·bΒ·c remain, in order.
t has no x at all, so s can't be formed.
- β’ 0 β€ s.length β€ 100
- β’ 0 β€ t.length β€ 10^4
- β’ s and t contain only lowercase English letters
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
At each matching pair, try both: use t[j] for s[i], or skip t[j]
dp[i][j] = first i chars of s fit inside the first j chars of t
j walks through t; i moves forward only when s[i] == t[j]
Write a function tryMatch(i, j) = "can s[iβ¦] be found inside t[jβ¦]?". If s is used up, yes. If t is used up, no. Otherwise, if s[i] == t[j] we may use this character (move both) β and if that fails we also try skipping t[j] (move only j).
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.