0%

392. Is Subsequence

O(t.length)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool isSubsequence(string s, string t) {
int m = s.length(), n = t.length(), i = 0, j = 0;
if (m > n) return false;
for (; i < m && j < n; ++j) {
if (s[i] == t[j]) {
++i;
}
}
return i == m;
}
};
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool isSubsequence(string s, string t) {
int i_t = 0;
for (auto&& c : s) {
while (i_t < t.length() && t[i_t] != c) ++i_t;
if (i_t++ >= t.length()) return false;
}
return true;
}
};