I wrote this code in my computer's IDE and passed all test cases I could think of. However, after testing it out on AlgoExpert and LeetCode, I'm getting a Time Out error. I've wracked my brain trying to come up any test cases I could've missed, but cannot seem to think of any. I've also tried testing out all the test cases they give me in AlgoExpert. Hopefully I'm not missing anything obvious... Any tips from someone will be much appreciated!
Given 2 non-empty array of integers, write a function that determines whether the second array is a subsequence of the first. Note that a single number in an array or the array itself are both valid subsequences of the array.
function isValidSubsequence(array, sequence) {
if (sequence.length > array.length) return false;
let s = 0;
let a = 0;
while (s < sequence.length){
if (array[a] === sequence[s]){
s++;
a++
}
else a++;
if (s !== sequence.length - 1 && a === array.length - 1) return false;
}
return true;
}