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
From: LeetCode
Link: 392. Is Subsequence
bool isSubsequence(char* s, char* t) {
int sIndex = 0; // Index for string s
int tIndex = 0; // Index for string t
// Iterate through both strings
while (s[sIndex] != '\0' && t[tIndex] != '\0') {
if (s[sIndex] == t[tIndex]) {
// Move to next character in s if there's a match
sIndex++;
}
// Always move to next character in t
tIndex++;
}
// If we've reached the end of s, then it's a subsequence of t
return s[sIndex] == '\0';
}