LeetCode 392: Is Subsequence is an Easy two pointers problem. This Python walkthrough develops a subsequence pointer solution, ties every code decision to a concrete invariant, and includes the regression check used before publication.
This independent guide paraphrases the task rather than reproducing LeetCode’s prompt. Use the official page for the exact statement, examples, constraints, and submission runner.
| Difficulty | Easy |
|---|---|
| Topic | Two Pointers |
| Reusable pattern | subsequence pointer |
| Complexity | O(n) time and O(1) extra space |
Recognizing the pattern
Two pointers are useful when one comparison lets you permanently discard one side of the remaining search range.
For this problem specifically, advance the target pointer only when the current source character matches it. The invariant worth writing beside the code is: The matched prefix is the longest prefix of s that can be formed from the processed part of t.
Step-by-step algorithm
- Identify the input state consumed by
isSubsequence(s, t)and initialize the data required by the subsequence pointer pattern. - Advance the target pointer only when the current source character matches it.
- After each update, verify the page’s central invariant: The matched prefix is the longest prefix of s that can be formed from the processed part of t.
- Finish only after the boundary behavior is covered: An empty s always succeeds; repeated characters must preserve order.
Python solution
class Solution:
def isSubsequence(self, s, t):
i = 0
for char in t:
if i < len(s) and s[i] == char:
i += 1
return i == len(s)Reading the implementation
The main entry point is isSubsequence(s, t). The named working state includes i; those variables make the subsequence pointer state visible instead of hiding it in incidental control flow.
A single main loop advances the algorithm, which is the key reason the traversal does not revisit already resolved input unnecessarily. In concrete terms, advance the target pointer only when the current source character matches it.
Correctness argument
Initialization. Before the scan begins, the unresolved range contains every candidate and the processed range is empty, so no answer has been lost.
Preservation. Advance the target pointer only when the current source character matches it. The chosen movement discards only candidates that cannot improve the answer; afterward, the matched prefix is the longest prefix of s that can be formed from the processed part of t.
Termination. A boundary advances on every iteration. Once the active range is exhausted, every viable candidate was either measured or safely eliminated, so the stored result is optimal.
Complexity and trade-offs
O(n) time and O(1) extra space. The auxiliary-space figure excludes the returned output unless the output itself is the structure being built.
A quadratic pair scan is easier to invent but repeats comparisons that pointer movement can eliminate. That comparison is useful in an interview because it explains why the final implementation is preferable, not merely that it passes.
Regression check
The published implementation belongs to a 100-problem suite that is compiled and exercised behaviorally before deployment.
One reference assertion from that suite is shown below. It targets the normal path while the edge conditions in the next section cover the failure-prone boundaries.
assert Solution().isSubsequence("abc","ahbgdc")Common mistakes and edge cases
- Problem-specific boundary: An empty s always succeeds; repeated characters must preserve order.
- Pattern-level pitfall: Move the pointer justified by the comparison, and state whether the active interval includes or excludes each endpoint.
- Invariant check: after every update, confirm that the matched prefix is the longest prefix of s that can be formed from the processed part of t.
Interview review checklist
- Explain why subsequence pointer matches the structure of this input.
- State the invariant in one sentence before tracing code: The matched prefix is the longest prefix of s that can be formed from the processed part of t.
- Derive O(n) time and O(1) extra space from how many times each element or state is visited.
- Test the boundary explicitly: An empty s always succeeds; repeated characters must preserve order.
Browse the searchable 100 LeetCode Python Solutions hub. Previous: 125. Valid Palindrome · Next: 167. Two Sum II – Input Array Is Sorted