The key insight for this problem is to iterate through both strings simultaneously, matching characters from s
with
characters from t
in order. If we can match all characters from s
in the same order as they appear in t
, then s
is a subsequence of t
.
- Use two pointers,
i
for strings
andj
for stringt
. - Iterate through both strings:
- If the current characters match, move both pointers forward.
- If they don't match, only move the pointer for
t
forward.
- Continue until we reach the end of either string.
- If we've matched all characters in
s
(i.e.,i
has reached the end ofs
), thens
is a subsequence of `t.
- Time Complexity:
O(n)
, wheren
is the length oft
. In the worst case, we iterate through all characters oft
once. - Space Complexity:
O(1)
, as we only use two integer pointers, regardless of the input size.
The solution efficiently checks if s
is a subsequence of t
using a two-pointer approach. By iterating through both
strings with pointers i
and j
, it confirms if characters of s
appear in the same order within t
. The solution
handles edge cases, such as when s
is empty (always a subsequence) or when t
is too short to contain all characters
of s
. The algorithm operates in linear time, making it suitable for large inputs while maintaining constant space
usage, ensuring optimal performance.