Is Subsequence

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Is Subsequence

Approach

Use two pointers, one for each string. Advance the pointer for t on every iteration. Only advance the pointer for s when the characters match. If all characters in s are matched by the end, s is a subsequence of t.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Same Direction)

Why It Works

A subsequence preserves order but not contiguity. By greedily matching each character of s to the earliest possible position in t, we determine if the ordering can be preserved.

Updated Feb 2026