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.
tech-interview-handbook/apps/website/experimental/utilities/javascript/isSubsequence.js

20 lines
610 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;
7 years ago
}
}
return matchedLength === s.length;
7 years ago
}
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);