325/670

325. Is Subsequence

Easy

You are given two strings s and t. Decide whether s is a subsequence of t — that is, whether you could obtain s by deleting zero or more characters from t while keeping the surviving characters in their original left-to-right order.

Return true if s can be threaded through t this way, and false otherwise.

For example, "cde" is a subsequence of "candied" (keep the letters at positions 0, 3, and 5), but "dna" is not — all three letters occur in "candied", just never in that order.

Example 1:

Input: s = "cde", t = "candied"

Output: true

Explanation: Delete "a", "n", "i", and the final "d" from "candied" and "cde" remains, in order.

Example 2:

Input: s = "dna", t = "candied"

Output: false

Explanation: After matching "d" at position 3, no "n" appears to its right — the letters exist but not in the required order.

Constraints:

  • 0 ≤ s.length ≤ 100
  • 0 ≤ t.length ≤ 10⁴
  • s and t consist of lowercase English letters.

Hints:

Order is the whole problem — counting letters is not enough. Try to consume s from left to right while reading t once.

Keep one pointer i into s. Scan t; whenever the current character of t equals s[i], advance i. Taking the earliest possible match never hurts, and s is a subsequence exactly when i falls off the end of s.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: s = "cde", t = "candied"

Expected output: true