1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public boolean isSubsequence(String s, String t) {
if (s.length() > t.length()) {
return false;
}
int ptrOne = 0, ptrTwo = 0;
while (ptrOne < s.length() && ptrTwo < t.length()) {
if (s.charAt(ptrOne) == t.charAt(ptrTwo)) {
ptrOne += 1;
}
ptrTwo += 1;
}
return ptrOne == s.length();
}
}

双指针即可.

时间复杂度: O(m) m是t的length.
空间复杂度: O(1)