#392

Is Subsequence

Easy Same direction

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether s can be obtained from t by deleting some characters without changing the order of the rest.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can you find s's letters inside t in the same order?

Why this works: walk t once; whenever its letter matches the next needed letter of s, move forward in s.

Example: s = "abc", t = "ahbgdc"

a ✓, h, b ✓, g, d, c ✓ → all found → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using indexOf for each character from the start of t each time, which ignores order.