Updated code that only uses one while loop: let isSubsequence = function(s, t) { if (s.length === 0) { return true; } let pointer1 = 0; let pointer2 = 0; while (pointer1 < s.length && pointer2 < t.length) { if (t.charAt(pointer2) === s.charAt(pointer1)) { pointer1++; } pointer2++; } return pointer1 === s.length; }; Also, if there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!
Updated code that only uses one while loop:
let isSubsequence = function(s, t) {
if (s.length === 0) {
return true;
}
let pointer1 = 0;
let pointer2 = 0;
while (pointer1 < s.length && pointer2 < t.length) {
if (t.charAt(pointer2) === s.charAt(pointer1)) {
pointer1++;
}
pointer2++;
}
return pointer1 === s.length;
};
Also, if there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!
If S is acb and T is abcd , S is not a subsequence of T right? But it will fail here right,?
Hm. Why would it fail?
@@TerribleWhiteboard My bad.. It will not fail..By the way , u are an awesome problem solver and tutor.. Thank you !
Thanks!
@@TerribleWhiteboard but what if abc and abdxi you return true cuz pointer1 === s.length but it is not
Great Explanation...thanks buddy
Thanks!
Thanks for the detail.
No problem!
Great solution!
Thanks!
Thanks for your explanations .Really helped alot.
You're welcome!
I find your videos so helpful, thank you!
Thanks for the explanation.
You're welcome!
Thanks for all this . Easy and straight forward :)
You're welcome!
that nested while loop was unnecessary.
True. I updated the code in the description. Thanks.
Your example it is to complicated...
function isSub(t, str) {
if(t === '') {
return true;
}
if(t.length > str.length) {
return false;
}
for(let i = 0; i < t.length; i++) {
if(str.indexOf(t[i]) === -1) {
return false;
}
}
return true;
}
var isSubsequence = function(s, t) {
let pS = 0, pT = 0;
while(!!s[pS] && !!t[pT]){
if(s[pS] === t[pT]){
pS++;
}
pT++;
}
return (pS - 1) === (s.length - 1)
};