Posted onEdited onInLeetCodeDisqus: Symbols count in article: 482Reading time ≈1 mins.
O(t.length)
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: boolisSubsequence(string s, string t){ int m = s.length(), n = t.length(), i = 0, j = 0; if (m > n) returnfalse; 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
classSolution { public: boolisSubsequence(string s, string t){ inti_t = 0; for (auto&& c : s) { while (i_t < t.length() && t[i_t] != c) ++i_t; if (i_t++ >= t.length()) returnfalse; } returntrue; } };