- 双指针
class Solution {
public:
bool isSubsequence(string s, string t) {
int m = s.size(), n = t.size();
int i = 0, j = 0;
while (i < m && j < n) {
if (s[i] == t[j]) {
++i;
}
++j;
}
return i == m;
}
};
- 队列
class Solution {
public:
bool isSubsequence(string s, string t) {
queue<int> q;
for (auto e : s)
{
q.push(e);
}
for (auto e : t)
{
if (e == q.front())
{
q.pop();
}
}
return q.empty();
}
};