Check If a String Is a Subsequence of Another String
Implement isSubsequence
Given two strings
s and t, return true if s is a subsequence of t — meaning every character of s appears in t, in the same relative order, though not necessarily contiguously.
Searching for each character of s across all of t from scratch works, but it repeats work every scan already did. The two-pointerTwo PointerUsing two indices that move through one or more sequences to avoid redundant re-scanning. technique walks both strings forward together in a single pass: advance the pointer into t on every step, and advance the pointer into s only when the current characters match. Neither pointer ever moves backward, so t is scanned exactly once no matter how the characters line up.
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
Example 3:
Input: s = "", t = "abc"
Output: true
+ 8 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ s.length ≤ 100 - ●
0 ≤ t.length ≤ 10⁴ - ●
s and t consist of lowercase English letters only
s =
abc
t =
ahbgdc