Is Subsequence
EasyTop Interview 150 Two PointersWixTinkoffProblem
You are given two strings, s and t. Decide whether s can be
obtained from t by deleting zero or more characters of t without changing the order
of the ones that remain.
Return true if so, otherwise false. The kept characters do not need to be neighbours.
Constraints
- 1 ≤ s.length ≤ t.length ≤ 10⁴
- s and t contain lowercase English letters only
Examples: pick one to animate
Solutions
TimeO(n)
SpaceO(1)
Time: j walks across t once, and i only moves forward, so there are at most n steps where n is the length of t.
Space: Only two integer indexes are kept.
Scan t from left to right with j. Keep i on the next letter of
s that we still need. When t[j] equals s[i], advance i.
At the end, s is a subsequence exactly when i reached its length.