Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
Input: s = "abc", t = "ahbgdc"
Output: true
Input: s = "axc", t = "ahbgdc"
Output: false
s and t consist only of lowercase English letters.Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
To check whether a string s is a subsequence of t, we try to match the characters of s in order while scanning through t. We don’t need to modify either string or use extra space.
We use two pointers:
As we traverse t, we look for characters that match the current character in s. Whenever we find a match, we advance the pointer in s.
The pointer in t always moves because we are scanning the entire sequence.
If we manage to advance through all characters in s, then every character in s appears in the correct order in t, meaning s is a subsequence of t.
i for tracking the current character in s and j for scanning through tt using j.s[i] == t[j], increment i because we matched a character of s.j because we continue scanning t.i reaches the length of s, it means all characters in s have been matched correctly in order within t.true if all characters of s were matched; otherwise return false.