Is Subsequence

easy

Check if s is a subsequence of t

Is Subsequence

Key Insight

Two pointers: advance s-pointer only on match. If s-pointer reaches end, it's a subsequence.

Step 1Setup
s="abc", t="ahbgdc"
i
j
a
0
h
1
b
2
g
3
d
4
c
5
same direction

Is "abc" a subsequence of "ahbgdc"?

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Is Subsequence

Two pointers: advance s-pointer only on match. If s-pointer reaches end, it's a subsequence.

  1. Setup

    Is "abc" a subsequence of "ahbgdc"?

  2. First Match

    s[0]="a" matches t[0]="a". Advance both.

  3. No Match

    s[1]="b" ≠ t[1]="h". Advance j only.

  4. Match Found

    s[1]="b" matches t[2]="b". Advance both.

  5. Is Subsequence

    i reached end of s. "abc" IS a subsequence!