You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
640 B
20 lines
640 B
7 years ago
|
function isSubsequence(s, t) {
|
||
|
if (s.length > t.length) {
|
||
|
return false;
|
||
|
}
|
||
|
let matchedLength = 0;
|
||
|
for (let i = 0; i < t.length; i++) {
|
||
|
if (matchedLength < s.length && s[matchedLength] === t[i]) {
|
||
|
matchedLength += 1;
|
||
|
}
|
||
|
}
|
||
|
return matchedLength === s.length;
|
||
|
}
|
||
|
|
||
|
console.log(isSubsequence('abc', 'abcde') === true);
|
||
|
console.log(isSubsequence('abd', 'abcde') === true);
|
||
|
console.log(isSubsequence('abf', 'abcde') === false);
|
||
|
console.log(isSubsequence('abef', 'abcde') === false);
|
||
|
console.log(isSubsequence('abcdef', 'abcde') === false);
|
||
|
console.log(isSubsequence('a', 'abcde') === true);
|