https://leetcode.com/problems/is-subsequence/description/
bool isSubsequence(string s, string t) {
if(s.empty()) return true;
int i=0, j=0;
while(j < t.size()) {
if(s[i] == t[j]) {
i++;
j++;
}
else
j++;
}
return i == s.size();
}
or
bool isSubsequence(string s, string t) {
if(s.empty()) return true;
int i=0, j=0;
for(; i<s.size(); i++) {
while(j < t.size() && s[i] != t[j]) j++;
if(j==t.size()) return false;
j++;
}
return i == s.size();
}
No comments:
Post a Comment